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 unescaped version of the String to the function (i.e. this means converting \n
to a newline, and \\
to a single \
, etc).
Is there a function inside the Java API that does this? If not, can I obtain such functionality from some library? Obviously the Java compiler has to do this conversion.
String#translateEscapes
Modern Java offers the method String#translateEscapes
.
This feature arrived in Java 15, after a preview in Java 13 and 14.
This feature un-escapes:
\t
for Tab and \r
for Carriage Return.\0
through \377
as a code point equivalentThis method does not translate Unicode escapes such as "\u2022" for the BULLET character (•
).