关键是使用StringCollection,需要引入System.Collections.Specialized命名空间。
代码如下:
using System.Text; using System.Collections.Specialized; //去除重复的数据 StringBuilder sb = new StringBuilder(); sb.Append(1+","); sb.Append(2+","); sb.Append(3+","); sb.Append(1+","); sb.Append(3+","); StringCollection sc = new StringCollection(); string[] arr = sb.ToString().TrimEnd(',').Split(','); foreach (string str in arr) { if (!sc.Contains(str)) { sc.Add(str); } } StringBuilder sb2 = new StringBuilder(); foreach (string str in sc) { sb2.Append(str + ","); } return sb2.ToString().TrimEnd(',');