How to unescape a Java string literal in Java?

后端 未结 11 1818
庸人自扰
庸人自扰 2020-11-22 01:35

I\'m processing some Java source code using Java. I\'m extracting the string literals and feeding them to a function taking a String. The problem is that I need to pass the

11条回答
  •  礼貌的吻别
    2020-11-22 02:14

    org.apache.commons.lang3.StringEscapeUtils from commons-lang3 is marked deprecated now. You can use org.apache.commons.text.StringEscapeUtils#unescapeJava(String) instead. It requires an additional Maven dependency:

            
                org.apache.commons
                commons-text
                1.4
            
    

    and seems to handle some more special cases, it e.g. unescapes:

    • escaped backslashes, single and double quotes
    • escaped octal and unicode values
    • \\b, \\n, \\t, \\f, \\r

提交回复
热议问题