求一算法
string str= "a,bb,a,c,ff,bb ";
把上面的字符串转换成下面的:
str= "a,bb,c,ff ";
也就是去掉以“,”分割的重复字符串!
------解决方案--------------------用hashtable,或者list <T> 或者ArrayList等去掉重复项..
string str= "a,bb,a,c,ff,bb ";
string[] s = str.Split( ', ');
ArrayList list = new ArrayList();
foreach (string ss in s)
{
if (!list.Contains(ss))
{
list.Add(ss);
}
}
str = list[0].ToString();
for (int i = 1; i < list.Count; i++)
{
str += ", ";
str += list[i].ToString();
}
Console.WriteLine(str);
看看有没有更好的方法..
学习..
------解决方案--------------------string str= "a,bb,a,c,ff,bb ";
string[] arr = str.Split( ', ');
StringCollection sc = new StringCollection()
foreach (string s in arr)
{
if (!sc.Contains(s))
{
sc.Add(s);
}
}
String[] arr2 = new String[sc.Count];
sc.CopyTo(arr2, 0);
string strOut = string.Join( ", ",arr2);