C method of deleting duplicate characters in a string

  • 2020-12-22 17:45:10
  • OfStack

This article shows an example of how C# removes duplicate characters from a string. Share to everybody for everybody reference. Specific implementation methods are as follows:

#region  Delete duplicate characters   
string s = "sdfffffsrlkjesgljfdg03940864e5=_0R9DTGR98YUI\\|||'\\???fdf///"; 
Response.Write("<br/>String : " + s + "<br/>Result : "); 
IEnumerable<char> distinctList = s.Distinct(); 
foreach (char a in distinctList) 

    Response.Write(a.ToString()); 

 
// Use the removal method  
for (int i = 0; i < s.Length; 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("<hr/>Result : " + s); 
#endregion

Hopefully this article has helped you with your C# programming.


Related articles: