C#删除字符串中重复字符的方法

2021-04-23 13:53

阅读:718

#region 删除重复字符

string s = “sdfffffsrlkjesgljfdg03940864e5=_0R9DTGR98YUI\\|||’\\???fdf///”;

Response.Write(“
String:” + s + “
Result:”);

IEnumerable distinctList = s.Distinct();

foreach (char a in distinctList)

{

Response.Write(a.ToString());

}

//使用移除法

for (int i = 0; i

{

while (s.IndexOf(s.Substring(i, 1)) != s.LastIndexOf(s.Substring(i, 1)))

{

s = s.Remove(s.LastIndexOf(s.Substring(i, 1)), 1);

}

}

Response.Write(“


Result:” + s);

#endregion


评论


亲,登录后才可以留言!