I need to escape special characters in a
String
Escaper
Escaper escaper = Escapers.builder()
.addEscape('[', "\\[")
.addEscape(']', "\\]")
.build();
String escapedStr = escaper.escape("This is a [test]");
System.out.println(escapedStr);
// -> prints "This is a \[test\]"
String
Escaper
unescape()
Escaper
Escaper escaper = Escapers.builder()
.addEscape('@', " at ")
.addEscape('.', " dot ")
.build();
Escaper
Escaper escaper = Escapers.builder()
.addEscape('&', "&")
.addEscape('<', "<")
.addEscape('>', ">")
.build();
No, it does not. And apparently, this is intentional. Quoting from this discussion where Chris Povirk answered:
The use case for unescaping is less clear to me. It's generally not possible to even identify the escaped source text without a parser that understands the language. For example, if I have the following input:
String s = "foo\n\"bar\"\n\\";
Then my parser has to already understand
\n
,\"
, and\\
in order to identify that...foo\n\"bar\"\n\\
...is the text to be "unescaped." In other words, it has to do the unescaping already. The situation is similar with HTML and other formats: We don't need an unescaper so much as we need a parser.
So it looks like you'll have to do it yourself.