public class StringEscapeUtils extends Object
Escapes and unescapes String
s for
Java, Java Script, HTML and XML.
#ThreadSafe#
This code has been adapted from Apache Commons Lang 3.5.
Modifier and Type | Class and Description |
---|---|
static class |
StringEscapeUtils.Builder
Convenience wrapper for
StringBuilder providing escape methods. |
Modifier and Type | Field and Description |
---|---|
static CharSequenceTranslator |
ESCAPE_JAVA
Translator object for escaping Java.
|
Constructor and Description |
---|
StringEscapeUtils()
StringEscapeUtils instances should NOT be constructed in
standard programming. |
Modifier and Type | Method and Description |
---|---|
static StringEscapeUtils.Builder |
builder(CharSequenceTranslator translator)
Get a
StringEscapeUtils.Builder . |
static String |
escapeJava(String input)
Escapes the characters in a
String using Java String rules. |
public static final CharSequenceTranslator ESCAPE_JAVA
While escapeJava(String)
is the expected method of use, this
object allows the Java escaping functionality to be used
as the foundation for a custom translator.
public StringEscapeUtils()
StringEscapeUtils
instances should NOT be constructed in
standard programming.
Instead, the class should be used as:
StringEscapeUtils.escapeJava("foo");
This constructor is public to permit tools that require a JavaBean instance to operate.
public static StringEscapeUtils.Builder builder(CharSequenceTranslator translator)
StringEscapeUtils.Builder
.translator
- the text translatorStringEscapeUtils.Builder
public static final String escapeJava(String input)
Escapes the characters in a String
using Java String rules.
Deals correctly with quotes and control-chars (tab, backslash, cr, ff, etc.)
So a tab becomes the characters '\\'
and
't'
.
The only difference between Java strings and JavaScript strings is that in JavaScript, a single quote and forward-slash (/) are escaped.
Example:
input string: He didn't say, "Stop!" output string: He didn't say, \"Stop!\"
input
- String to escape values in, may be nullnull
if null string inputCopyright © 2005–2021. All rights reserved.