java replace German umlauts

后端 未结 8 784
爱一瞬间的悲伤
爱一瞬间的悲伤 2020-12-31 06:24

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

8条回答
  •  -上瘾入骨i
    2020-12-31 07:17

    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

提交回复
热议问题