Replace German characters (umlauts, accents) with english equivalents

40,266

The process is known as removing "diacritics" - see Removing diacritics (accents) from strings which uses the following code:

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();
}
Share:
40,266
jb.
Author by

jb.

Updated on October 16, 2020

Comments

  • jb.
    jb. over 3 years

    Replace German characters (umlauts, accents) with english equivalents

    I need to remove any german specific characters from various fields of text for processing into another system which wont accept them as valid.

    So the characters I am aware of are:

    ß ä ö ü Ä Ö Ü

    At the moment I have a bit of a manual way of replacing them:

    myGermanString.Replace("ä","a").Replace("ö","o").Replace("ü","u").....
    

    But I was hoping there was a simpler / more efficient way of doing it. Since I'll be doing it on thousands of strings per run, 99% of which will not contain these chars.

    Maybe a method involving some sort of CultureInfo?

    (for example, according to MS, the following returns the strings are equal

    String.Compare("Straße", "Strasse", StringComparison.CurrentCulture);
    

    so there must be some sort of conversion table already existing?)

  • ChrisF
    ChrisF over 12 years
    Can you summarise the post here. It helps to keep the information in one place and helps guard against link rot.
  • jb.
    jb. over 12 years
    What this doesnt work for is the 'ß' character - which is just returned as it was.
  • Alex Essilfie
    Alex Essilfie over 9 years
    @jb. I believe you'll have to do a hard replace of the German characters in order to achieve the desired effect. This may be the preferable method since one-character German letters with umlauts can be mapped to two-character non-umlaut versions. See the answers to the question linked in Joe's answer for solutions.