I have the following problem. I am trying to replace german umlauts like ä, ö, ü in java. But it simply does not work. Her
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 umlaute 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 umlaute
output = output.replace("Ü", "UE")
.replace("Ö", "OE")
.replace("Ä", "AE");
return output;
}
Source