日期:2014-05-18  浏览次数:20908 次

C# 字符串的处理(大学计算机课题目)
以下这个是大学计算机课上的一个题目:

现有3个string类型的字符串:
string a="A B C";
string b="A D E";
string c="B C F";

如何得出最后的结果:
string d="A B C D E F"?

------解决方案--------------------
C# code

string result = string.Join(" ", Regex.Split(a, @"\s+").Union(Regex.Split(b, @"\s+")).Union(Regex.Split(c, @"\s+")).Distinct());//A B C D E F

------解决方案--------------------
linq完爆
C# code

class Program
    {
        static void Main(string[] args)
        {
            var strA = "A B C";
            var strB = "A D E";
            var strC = "B C F";
            var aList = strA.Split(' ').ToList();
            var bList = strB.Split(' ').ToList();
            var cList = strC.Split(' ').ToList();
            aList.AddRange(bList);
            aList.AddRange(cList);
            var result = aList.Distinct();
            foreach (var temp in result)
            {
                Console.WriteLine(temp);
            }
            Console.ReadLine();
        }
    }

------解决方案--------------------
这样更优雅
C# code

 string a = "A B C";
 string b = "A D E";
 string c = "B C F";
 string d = string.Join(" ",(a + b + c).ToCharArray().Distinct().OrderBy(x => x).Select(x => x.ToString()).ToArray()).Trim();

------解决方案--------------------
比较蛋疼的方法
C# code
string d = String.Join(" ", a , b.Trim(a.ToArray()) , c.Trim((a + b).ToCharArray()));

------解决方案--------------------
http://blog.csdn.net/sheshou2/article/details/4766639
------解决方案--------------------
探讨
Regex reg = new Regex(@"(([A-Za-z]).*?)\2\s");

------解决方案--------------------
探讨

正在测试各位的答案,谢谢。

------解决方案--------------------
C# code

private void Form1_Load(object sender, EventArgs e)
        {
            string a = "A B C";
            string b = "A D E";
            string c = "B C F";

            IList<string> chars = new List<string>();

            this.FillList(chars, a);
            this.FillList(chars, b);
            this.FillList(chars, c);

            string result = string.Empty;

            foreach (string s in chars)
            {
                result += s + " ";
            }

            MessageBox.Show(result);
        }

        private void FillList(IList<string> list, string s)
        {
            string[] values = s.Split(' ');
            foreach (string c in values)
            {
                if (!list.Contains(c))
                {
                    list.Add(c);
                }
            }
        }