[ACCEPTED]-How do i replace accents (german) in .NET-diacritics

Accepted answer
Score: 39

If you need to use this on larger strings, multiple 3 calls to Replace() can get inefficient pretty quickly. You 2 may be better off rebuilding your string 1 character-by-character:

var map = new Dictionary<char, string>() {
  { 'ä', "ae" },
  { 'ö', "oe" },
  { 'ü', "ue" },
  { 'Ä', "Ae" },
  { 'Ö', "Oe" },
  { 'Ü', "Ue" },
  { 'ß', "ss" }
};

var res = germanText.Aggregate(
              new StringBuilder(),
              (sb, c) => map.TryGetValue(c, out var r) ? sb.Append(r) : sb.Append(c)
              ).ToString();
Score: 11

Do just want a mapping of german umlauts 3 to the two-letter (non-umlaut) variant? Here 2 you go; untested, but it handles all german 1 umlauts.

String replaceGermanUmlauts( String s ) {
    String t = s;
    t = t.Replace( "ä", "ae" );
    t = t.Replace( "ö", "oe" );
    t = t.Replace( "ü", "ue" );
    t = t.Replace( "Ä", "Ae" );
    t = t.Replace( "Ö", "Oe" );
    t = t.Replace( "Ü", "Ue" );
    t = t.Replace( "ß", "ss" );
    return t;
}
Score: 4

I can't think of any automatic way to do 4 this, so I believe you'd have to do it manually.

ie.

string GermanString = "äö";
GermanString = GermanString.Replace("ä", "ae");
GermanString = GermanString.Replace("ö", "oe");

How 3 many characters are there? All vowels, in 2 upper and lower case, so, 10? Shouldn't 1 be too much of a job.

Score: 0

How about using string.Replace:

string germanText = "Mötörhead";
string replaced = germanText.Replace("ö", "oe");

(okay, not 2 a real German word, but I couldn't resist)

You 1 can chain calls to Replace like this

someText.Replace("ö", "oe").Replace("ä", "ae").Replace("ö", "oe")...
Score: 0

This class removes diacritic characters 6 (é, ì, è, etc.) and replaces umlauts and 5 the German "ß" with their equivalents "ae 4 (ä)", "oe (ö)", "ue (ü)" and "ss (ß)".

public sealed class UmlautConverter
{
    private Dictionary<char, string> converter = new Dictionary<char, string>()
    {
        {  'ä', "ae" },
        {  'Ä', "AE" },
        {  'ö', "oe" },
        {  'Ö', "OE" },
        {  'ü', "ue" },
        {  'Ü', "UE" },
        {  'ß', "ss" }
    };

    string value = null;
    public UmlautConverter(string value)
    {
        if (!string.IsNullOrWhiteSpace(value))
        {
            this.value = value;
        }
    }
    public string RemoveDiacritics()
    {
        if (string.IsNullOrWhiteSpace(value))
        {
            return null;
        }

        string normalizedString = this.value.Normalize();

        foreach (KeyValuePair<char, string> item in this.converter)
        {
            string temp = normalizedString;
            normalizedString = temp.Replace(item.Key.ToString(), item.Value);
        }

        StringBuilder stringBuilder = new StringBuilder();

        for (int i = 0; i < normalizedString.Length; i++)
        {
            normalizedString = normalizedString.Normalize(NormalizationForm.FormD);
            string c = normalizedString[i].ToString();
            if (CharUnicodeInfo.GetUnicodeCategory(Convert.ToChar(c)) != UnicodeCategory.NonSpacingMark)
            {
                stringBuilder.Append(c);
            }
        }
        return stringBuilder.ToString();
    }

    public bool HasUmlaut()
    {
        if (string.IsNullOrWhiteSpace(value))
        {
            return false;
        }

        foreach (KeyValuePair<char, string> item in this.converter)
        {
            if (this.value.Contains(item.Key.ToString()))
            {
                return true;
            }
        }

        return false;
    }
}

Usage:

Console.WriteLine(new UmlautConverter("Nürnberger Straße").RemoveDiacritics()); // Nuernberger Strasse

        Console.WriteLine(new UmlautConverter("Größenwahn").RemoveDiacritics()); // Groessenwahn
        Console.WriteLine(new UmlautConverter("Übermut").RemoveDiacritics()); // UEbermut
        Console.WriteLine(new UmlautConverter("Università").RemoveDiacritics()); // Universita
        Console.WriteLine(new UmlautConverter("Perché").RemoveDiacritics());// Perche
        Console.WriteLine(new UmlautConverter("être").RemoveDiacritics()); // etre

There 3 is a minor bug in the "Übermut" case replacing 2 "Ü" with "UE" instead of Ue". But this can 1 be easily fixed. Enjoy :)

More Related questions