/egilh

Learning by doing

How to remove accents from strings in .NET

Posted on Wednesday, June 13, 2007 3:02 PM

A friend asked how to remove accents from strings in .NET 2.0. I found the code below on Michael Kaplan's blog.

The code uses String.Normalize() to get a normalized Unicode representation of the string where the base character and the accents are stored separately. It then loops on each character and ignores the accent mark characters so "àáåæéèøÜü" becomes "aaaæeeøUu".

public static String RemoveDiacritics(String s)       

{

    String normalizedString = s.Normalize(NormalizationForm.FormD);

    StringBuilder stringBuilder = new StringBuilder();

 

    for (int i = 0; i < normalizedString.Length; i++)

    {

        Char c = normalizedString[i];

        if (CharUnicodeInfo.GetUnicodeCategory(c) != UnicodeCategory.NonSpacingMark)

            stringBuilder.Append(c);

    }

    return stringBuilder.ToString();

}




Feel free to drop a few cents in the tip jar if this post saved you time and money

Post Comment
Title
 

Name
 

Url

Protected by Clearscreen.SharpHIPEnter the code you see:
Comment