字符串是以,相隔的数字的形式,如何去除其中相同的数字? 比如:1,2,2,6,78,9,10,6变为1,2,6,78,9,10这个如何写(c#) 解决方案 » 免费领取超大流量手机卡,每月29元包185G流量+100分钟通话, 中国电信官方发货 string source = "1,2,2,6,78,9,10,6"; string result = ""; System.Collections.Generic.List<string> stringList = new System.Collections.Generic.List<string>(); string[] strList = source.Split(',',StringSplitOptions.RemoveEmptyEntries); foreach (string str in stringList) { if (!stringList.Contains(str)) { stringList.Add(str); result += str + ","; } } //执行循环后result="1,2,6,78,9,10,6,"; result.Remove(result.Length - 1);//移除最后一个',',result="1,2,6,78,9,10,6"; string str = "1,2,2,6,78,9,10,6"; string newstr=""; List<string> str1 = new List<string>(); foreach (string s in str.Split(',')) { if (!str1.Contains(s)) str1.Add(s); } foreach (string s in str1) { newstr += s+","; } newstr = newstr.Remove(newstr.Length - 1); http://topic.csdn.net/t/20041210/14/3633655.html看这个帖子8楼的答案! 在后台程序运行时 弹出一个对话框 foreach迭代变量 Login控件问题 如何获得文件的属性如author,owner之类的 ASP.NET正规小问题 如何在转到另外的页面之前进行提示是否要保存 大家谈谈泛型编程在.net中的影响~~~见者有分~~~:) 请教一个C#的问题,在线等! 如何将函数赋值 程序能自动知道Cancel、Update、Delete吗? 在asp.net 用c#调用sql server2000存储过程,没有成功,请指教. 关于dropdownlist做成一条线效果
string source = "1,2,2,6,78,9,10,6";
string result = "";
System.Collections.Generic.List<string> stringList = new System.Collections.Generic.List<string>();
string[] strList = source.Split(',',StringSplitOptions.RemoveEmptyEntries);
foreach (string str in stringList)
{
if (!stringList.Contains(str))
{
stringList.Add(str);
result += str + ",";
}
}
//执行循环后result="1,2,6,78,9,10,6,";
result.Remove(result.Length - 1);//移除最后一个',',result="1,2,6,78,9,10,6";
string newstr="";
List<string> str1 = new List<string>(); foreach (string s in str.Split(','))
{
if (!str1.Contains(s))
str1.Add(s);
}
foreach (string s in str1)
{
newstr += s+",";
}
newstr = newstr.Remove(newstr.Length - 1);
看这个帖子8楼的答案!