Search code examples
javabibtex

Replacing german umlauts generated by latex or bibtex tool in java?


I want to replace the german umlauts generated by a Citavi-Bibtex-Export-Tool. For example one reference string input is J{\"o}rgand I want Jörg as a result. After inspecting my JUnit-Test the result of my method was J{"o}rg - what went wrong?

public String replaceBibtexMutatedVowels(String str){
    CharSequence target = "{\\\"o}";
    CharSequence replacement = "ö";
    str.replace(target, replacement);
    return str;
}

UPDATE: Thanks guys - I was able to master german umlauts - unfortunately Bibtex escapes quotation marks with {\dg} - I was not able to create the corresponding java code.

    String afterDg = "";
    CharSequence targetDg = "{\\dg}";
    CharSequence replacementDg = "\"";
    afterDg = afterAe.replace(targetDg, replacementDg);
    newStringInstance = afterDg;
    return newStringInstance;

Solution

  • Basically, you are doing all right, but:

     str.replace(target, replacement);
    

    must be replaced with

     str = str.replace(target, replacement);
    

    because replace doesn't change the string itself, but returns a "replaced string".

    P.S.: German has more special characters than "ö"; you're missing "ä", "ü" (and their corresponding capital letters), "ß" etc.

    And here's my test code:

    package test;
    
    public class Test {
    
        public static void main(String[] args) throws Exception {
            String latexText = "J{\\\"o}rg";
            String normalText = replaceBibtexMutatedVowels(latexText);
            System.out.println(latexText);
            System.out.println(normalText);
        }
    
        public static String replaceBibtexMutatedVowels(String str) {
            CharSequence target = "{\\\"o}";
            CharSequence replacement = "ö";
            str = str.replace(target, replacement);
            return str;
        }
    
    }