想实现什么效果。字符串比较不要split,会产生很多的字符串。效率很低。

解决方案 »

  1.   

    楼主为什么非要split之后在比较
      

  2.   

    本人鲁钝,没想到啥方法能比split开销更低的数据源初始化方法,只能在后续的比较过程中优化了先把两个字符串分割成List<string>;
    再将两个List<string>分别进行排序,用sort方法;这样一来,由于两个List都是经过排序的,就能应用算法,进行更快速的进行匹配了。就是不知道sort的成本怎么样,建议楼主试一下
      

  3.   

    这个要看你如何匹配了。比如
    AA,BB,CC,DD,EE,FF和AA,CC,DD,EE,FF
    需要跳过一个 BB继续匹配,还是顺序错误都算不匹配?不知道你的规则。
      

  4.   

    用归并排序方法,应该是效率比较高的一种算法了。        private void btnChargeSet_Click(object sender, EventArgs e)
            {
                List<string> root=new List<string>();
                root.Add("10072714211596876194");
                root.Add("10072714211596872927");
                root.Add("10072714211596879733");
                root.Add("abc");
                root.Add("defg");
                root.Add("asdfasdf");            List<string> source=new List<string>();
                source.Add("10072714211596876194");
                source.Add("10072714211596872927");
                source.Add("10072714211596876234");
                source.Add("abc");
                source.Add("abdefg");
                source.Add("asdfasdf");            List<string> remove;
                List<string> add;
                FindDistinct(root, source, out remove, out add);
            }        /// <summary>
            /// 归并排序: 查找两个集合中的不同数据
            /// </summary>
            /// <param name="root">源数据集合</param>
            /// <param name="source">新数据集合</param>
            /// <param name="remove">需在源数据中移除的集合</param>
            /// <param name="add">需在源数据中添加的集合</param>
            public void FindDistinct(List<string> root, List<string> source, out List<string> remove, out List<string> add)
            {
                remove = new List<string>();
                add = new List<string>();            root.Sort();
                source.Sort();
                foreach (string str in root) Console.WriteLine(str);
                Console.WriteLine("\r\n");
                foreach (string str in source) Console.WriteLine(str);
                Console.WriteLine("\r\n");            int i = 0, j = 0;
                while (i < root.Count && j < source.Count)
                {
                    switch (root[i].CompareTo(source[j]))
                    {
                        case -1:
                            remove.Add(root[i]); i++;
                            break;
                        case 0:
                            i++; j++;
                            break;
                        case 1:
                            add.Add(source[j]); j++;
                            break;
                    }
                }            if (i < root.Count)
                {
                    for (int m = i; m < root.Count; m++) remove.Add(root[m]);
                }
                else if (j < source.Count)
                {
                    for (int m = j; m < source.Count; m++) add.Add(source[m]);
                }            Console.WriteLine("\r\nroot中不同的数据:");
                foreach (string str in remove) Console.WriteLine(str);            Console.WriteLine("\r\nsource中不同的数据:");
                foreach (string str in add) Console.WriteLine(str);
            }
      

  5.   

    输出结果:
    ......root中不同的数据:
    10072714211596879733
    defgsource中不同的数据:
    10072714211596876234
    abdefg
      

  6.   

    public IEnumerable<string> CompareTwoString(string a,string b)
    {
      var retVal=(from c in a.Split() select c).Intersect(from e in b.Split() select e);
      return retVal;
    }打完收工!!
      

  7.   

    不要用数组或泛型,用HashTable