java replace German umlauts

This finally worked for me:

private static String[][] UMLAUT_REPLACEMENTS = { { new String("Ä"), "Ae" }, { new String("Ü"), "Ue" }, { new String("Ö"), "Oe" }, { new String("ä"), "ae" }, { new String("ü"), "ue" }, { new String("ö"), "oe" }, { new String("ß"), "ss" } };
public static String replaceUmlaute(String orig) {
    String result = orig;

    for (int i = 0; i < UMLAUT_REPLACEMENTS.length; i++) {
        result = result.replace(UMLAUT_REPLACEMENTS[i][0], UMLAUT_REPLACEMENTS[i][1]);
    }

    return result;
}

So thanks to all your answers and help. It finally was a mixture of nafas(with the new String) and Joop Eggen(the correct replace-Statement). You got my upvote thanks a lot!


i had to modify the answer of user1438038:

private static String replaceUmlaute(String output) {
    String newString = output.replace("\u00fc", "ue")
            .replace("\u00f6", "oe")
            .replace("\u00e4", "ae")
            .replace("\u00df", "ss")
            .replaceAll("\u00dc(?=[a-z\u00e4\u00f6\u00fc\u00df ])", "Ue")
            .replaceAll("\u00d6(?=[a-z\u00e4\u00f6\u00fc\u00df ])", "Oe")
            .replaceAll("\u00c4(?=[a-z\u00e4\u00f6\u00fc\u00df ])", "Ae")
            .replace("\u00dc", "UE")
            .replace("\u00d6", "OE")
            .replace("\u00c4", "AE");
    return newString;
}

This should work on any target platform (i had problems on a tomcat on windows).


Your code looks fine, replaceAll() should work as expected.

Try this, if you also want to preserve capitalization (e.g. ÜBUNG will become UEBUNG, not UeBUNG):

private static String replaceUmlaut(String input) {
 
     // replace all lower Umlauts
     String output = input.replace("ü", "ue")
                          .replace("ö", "oe")
                          .replace("ä", "ae")
                          .replace("ß", "ss");
 
     // first replace all capital Umlauts in a non-capitalized context (e.g. Übung)
     output = output.replaceAll("Ü(?=[a-zäöüß ])", "Ue")
                    .replaceAll("Ö(?=[a-zäöüß ])", "Oe")
                    .replaceAll("Ä(?=[a-zäöüß ])", "Ae");
 
     // now replace all the other capital Umlauts
     output = output.replace("Ü", "UE")
                    .replace("Ö", "OE")
                    .replace("Ä", "AE");
 
     return output;
 }

Source

Tags:

Java