Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

java replace German umlauts

Tags:

java

I have the following problem. I am trying to replace german umlauts like ä, ö, ü in java. But it simply does not work. Here is my code:

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

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

    return result;
}

An ä remains an ä and so on. I do not know if this issue has something to do with encoding, but the String contains the exact character I am trying to replace.

Thank you in advance

like image 908
user2841991 Avatar asked Sep 21 '15 13:09

user2841991


1 Answers

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).

like image 78
dermoritz Avatar answered Sep 20 '22 19:09

dermoritz