日期:2014-05-17  浏览次数:21383 次

字符串数组去重 比较
有一个字符串 string a="123,123,12,124,125,124,"

怎么去掉重复的123和124然后重新得到一个字符串:string b="123,12,124,125,"


帮忙了 用 C#就好。

------解决方案--------------------
以逗号split,然后循环添加到List,天津前判断是否已经存在
------解决方案--------------------

C# code

        string a = "123,123,12,124,125,124,";

        string[] result = a.Split(',').Distinct().ToArray();

        string b = string.Join(",", result);

//结果b等于:123,12,124,125,

------解决方案--------------------
C# code
            string str="123,123,12,124,125,124,";
            Regex reg = new Regex(@"((\d+,).*?)\2");
            while (str != (str = reg.Replace(str, "$1"))) ;
            Console.WriteLine(str);

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

引用:
C# code
while (str != (str = reg.Replace(str, "$1"))) ;
这是个什么循环?

------解决方案--------------------
string a = "123,123,12,124,125,124,";
string[] str = a.Split(',');

IEnumerable<string> ling = (from b in str select b).Distinct();
StringBuilder strbuil = new StringBuilder(100);
foreach (string s in ling)
{
strbuil.Append(s);
}