~fhusson

How to remove Diacritics in Dotnet

If you need to remove diacritics from a string you can do a lot’s of replace like this :

string inputString = "HéllÕ Wôrld !";
var result = inputString.Replace('À', 'A');

Repeat for ÀÁÂÃÄâãäàáÈÉÊËêëèéÌÍÎÏîïìíÒÓÔÖôõöòóÙÚÛÜûüùúÝýÑñç and maybe miss some exotic chars or the Õ in this list !
or you can use this function

public static string RemoveDiacritics(string inputString)
{
    //!\\ Warning 'œ' will be replaced with a 'o' not an 'oe'
    String normalizedString = inputString.Normalize(NormalizationForm.FormD);
    StringBuilder stringBuilder = new StringBuilder();
    for (int i = 0; i < normalizedString.Length; i++)
    {
        Char c = normalizedString[i];
        if (System.Globalization.CharUnicodeInfo.GetUnicodeCategory(c) != System.Globalization.UnicodeCategory.NonSpacingMark)
            stringBuilder.Append(c);
    }
    return stringBuilder.ToString();
}
Discuss on Twitter