且构网

分享程序员开发的那些事...
且构网 - 分享程序员编程开发的那些事

如何删除逗号分隔的重复字符串值。

更新时间:2023-11-07 20:49:34

在Anuja解决方案之上。



试试这个简单的。



On top of Anuja solution.

Try this simple one.

string ReturnStr = "2,5,12,35,2,12,78,45,35,78";
        string temp="";
        ReturnStr.Split(',').Distinct().ToList().ForEach(k => temp += k + ",");
        ReturnStr = temp.Trim(',');


,甚至更简单。我接受了答案2并将其简化为





and even simpler one. I took answer 2 and simplified it


string ReturnStr = "2,5,12,35,2,12,78,45,35,78";
string temp = string.Join(",", ReturnStr.Split(',').Distinct().ToArray());


试试这个

Try this
string ReturnStr = "2,5,12,35,2,12,78,45,35,78";
            string[] words = ReturnStr.Split(',');
            List<string> list = new List<string>();
            foreach (string word in words)
            {
                if ( ! list.Contains(word))
                    list.Add(word);
            }



更新

试试这个


Updated
try this also

if (ReturnStr .substring(ReturnStr .length-1, ReturnStr .length) == ",") {
        ReturnStr = ReturnStr .substring(0, ReturnStr .length-1);
    }
    TextBox1.Text = ReturnStr ;