如何去掉重复的数字?
string aa= "11,21,31,11,21,25,31 "
如何把重复的数字只保留一个,谢谢
------解决方案--------------------终于坐沙发了
------解决方案--------------------用Split()把字符串分割,在做判断
------解决方案--------------------/// <summary>
/// 替换重复的数组元素
/// </summary>
/// <param name= "ObjStr "> 字符串 </param>
/// <param name= "separator "> 字符串中的分隔符 </param>
/// <returns> 不含重复的数组元素 </returns>
public static string RWDistinct(string ObjStr, string separator)
{
int i, s;
string sResult = " ";
if (ObjStr == string.Empty) { sResult = " "; }
else
{
string[] arrStr = System.Text.RegularExpressions.Regex.Split(ObjStr, separator);
for (i = 0; i < arrStr.Length; i++)
{
if (i == 0) { sResult = arrStr[0]; }
else if (i == 1)
{ sResult += (arrStr[1] == sResult) ? (string.Empty) : (separator + arrStr[1]); }
else
{
bool StrExist = false;
string[] resultArray = System.Text.RegularExpressions.Regex.Split(sResult, separator);
for (s = 0; s < resultArray.Length; s++)
{
if (resultArray[s] == arrStr[i])
{ StrExist = true; break; }
}
sResult += StrExist ? string.Empty : (separator + arrStr[i]);
}
}
}
return sResult;
}
------解决方案--------------------string aa = "11,21,31,11,21,25,31 ";
string[] temp = aa.Split( ' ');
List <string> list = new List <string> ();
for (int i = 0; i < temp.Length; i++)
{
if (!list.Contains(temp[i]))
list.Add(temp[i]);
}
string ret = " ";
for (int i = 0; i < list.Count; i++)
{
if (i != list.Count - 1)
ret += list[i] + ", ";
else
ret += list[i];
}
return ret;
------解决方案--------------------没抢到沙发...
------解决方案--------------------string aa= "11,21,31,11,21,25,31 ";
string[] strS = aa.Split( ', ');
ArrayList array = new ArrayList();
foreach(string str in strS)
{
if (!array.Contains(str))
array.Add(str);
}
------解决方案--------------------String aa = "11,21,31,11,21,25,31 ";
String[] arr = aa.Split( ', ');
List <String> list = new List <string> ();
foreach (String s in arr)
{
if (!list.Contains(s))
list.Add(s);
}
String result = String.Join( ", ", list.ToArray());