去掉數組中重複元素以及字符串重複字符

//過濾數組中重複元素
ArrayList arryList = new ArrayList();
arryList.Add( "aa ");
arryList.Add( "bb");
arryList.Add( "cc");
arryList.Add( "dd");
arryList.Add( "aa ");
ArrayList arryListNew = new ArrayList();
foreach (object ob in arryList)
{
if (!arryListNew.Contains(ob))
{
arryListNew.Add(ob);
}

arryList=arryListNew;
}


//過濾字符串中重複元素
public static string FilterRepetitionString(string str)
{
str += ", ";
string str1 = " ";
while(str.Length> 0)
{
string str2 = str.Substring(0,str.IndexOf( ", ")+1);
str1 += str2;
str = ", " + str;
while(str.IndexOf( ", "+str2)> =0)
{
str = str.Replace( ", "+str2, ", ");
}
str = str.Remove(0,1);
}
str1 = str1.Substring(0,str1.Length-1);
return str1;
}
 

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章