StringEscapeUtils¶
- @Deprecated public class org.apache.commons.lang3.StringEscapeUtils[source]¶
Deprecated: As of 3.6, use Apache Commons Text StringEscapeUtils instead.
Escapes and unescapes
Strings for Java, Java Script, HTML and XML.#ThreadSafe#
- Since:
2.0
Constructors¶
Deprecated. |
- @Deprecated public StringEscapeUtils()[source]¶
Deprecated: TODO Make private in 4.0.
StringEscapeUtilsinstances 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.
Fields and constants¶
Translator object for escaping individual Comma Separated Values. |
|
Translator object for escaping EcmaScript/JavaScript. |
|
Translator object for escaping HTML version 3.0. |
|
Translator object for escaping HTML version 4.0. |
|
Translator object for escaping Java. |
|
Translator object for escaping Json. |
|
Deprecated. Translator object for escaping XML. |
|
Translator object for escaping XML 1.0. |
|
Translator object for escaping XML 1.1. |
|
Translator object for unescaping escaped Comma Separated Value entries. |
|
Translator object for unescaping escaped EcmaScript. |
|
Translator object for unescaping escaped HTML 3.0. |
|
Translator object for unescaping escaped HTML 4.0. |
|
Translator object for unescaping escaped Java. |
|
Translator object for unescaping escaped Json. |
|
Translator object for unescaping escaped XML. |
- public static final CharSequenceTranslator ESCAPE_CSV[source]¶
Translator object for escaping individual Comma Separated Values. While
#escapeCsv(String)is the expected method of use, this object allows the CSV escaping functionality to be used as the foundation for a custom translator.- Since:
3.0
- public static final CharSequenceTranslator ESCAPE_ECMASCRIPT[source]¶
Translator object for escaping EcmaScript/JavaScript. While
#escapeEcmaScript(String)is the expected method of use, this object allows the EcmaScript escaping functionality to be used as the foundation for a custom translator.- Since:
3.0
- public static final CharSequenceTranslator ESCAPE_HTML3[source]¶
Translator object for escaping HTML version 3.0. While
#escapeHtml3(String)is the expected method of use, this object allows the HTML escaping functionality to be used as the foundation for a custom translator.- Since:
3.0
- public static final CharSequenceTranslator ESCAPE_HTML4[source]¶
Translator object for escaping HTML version 4.0. While
#escapeHtml4(String)is the expected method of use, this object allows the HTML escaping functionality to be used as the foundation for a custom translator.- Since:
3.0
- public static final CharSequenceTranslator ESCAPE_JAVA[source]¶
Translator object for escaping 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.- Since:
3.0
- public static final CharSequenceTranslator ESCAPE_JSON[source]¶
Translator object for escaping Json. While
#escapeJson(String)is the expected method of use, this object allows the Json escaping functionality to be used as the foundation for a custom translator.- Since:
3.2
- @Deprecated public static final CharSequenceTranslator ESCAPE_XML[source]¶
Deprecated: Use
#ESCAPE_XML10or#ESCAPE_XML11instead.Translator object for escaping XML. While
#escapeXml(String)is the expected method of use, this object allows the XML escaping functionality to be used as the foundation for a custom translator.- Since:
3.0
- public static final CharSequenceTranslator ESCAPE_XML10[source]¶
Translator object for escaping XML 1.0. While
#escapeXml10(String)is the expected method of use, this object allows the XML escaping functionality to be used as the foundation for a custom translator.- Since:
3.3
- public static final CharSequenceTranslator ESCAPE_XML11[source]¶
Translator object for escaping XML 1.1. While
#escapeXml11(String)is the expected method of use, this object allows the XML escaping functionality to be used as the foundation for a custom translator.- Since:
3.3
- public static final CharSequenceTranslator UNESCAPE_CSV[source]¶
Translator object for unescaping escaped Comma Separated Value entries. While
#unescapeCsv(String)is the expected method of use, this object allows the CSV unescaping functionality to be used as the foundation for a custom translator.- Since:
3.0
- public static final CharSequenceTranslator UNESCAPE_ECMASCRIPT[source]¶
Translator object for unescaping escaped EcmaScript. While
#unescapeEcmaScript(String)is the expected method of use, this object allows the EcmaScript unescaping functionality to be used as the foundation for a custom translator.- Since:
3.0
- public static final CharSequenceTranslator UNESCAPE_HTML3[source]¶
Translator object for unescaping escaped HTML 3.0. While
#unescapeHtml3(String)is the expected method of use, this object allows the HTML unescaping functionality to be used as the foundation for a custom translator.- Since:
3.0
- public static final CharSequenceTranslator UNESCAPE_HTML4[source]¶
Translator object for unescaping escaped HTML 4.0. While
#unescapeHtml4(String)is the expected method of use, this object allows the HTML unescaping functionality to be used as the foundation for a custom translator.- Since:
3.0
- public static final CharSequenceTranslator UNESCAPE_JAVA[source]¶
Translator object for unescaping escaped Java. While
#unescapeJava(String)is the expected method of use, this object allows the Java unescaping functionality to be used as the foundation for a custom translator.- Since:
3.0
- public static final CharSequenceTranslator UNESCAPE_JSON[source]¶
Translator object for unescaping escaped Json. While
#unescapeJson(String)is the expected method of use, this object allows the Json unescaping functionality to be used as the foundation for a custom translator.- Since:
3.2
- public static final CharSequenceTranslator UNESCAPE_XML[source]¶
Translator object for unescaping escaped XML. While
#unescapeXml(String)is the expected method of use, this object allows the XML unescaping functionality to be used as the foundation for a custom translator.- Since:
3.0
Methods¶
Returns a |
|
Escapes the characters in a |
|
Escapes the characters in a |
|
Escapes the characters in a |
|
Escapes the characters in a |
|
Escapes the characters in a |
|
Deprecated. Escapes the characters in a |
|
Escapes the characters in a |
|
Escapes the characters in a |
|
Returns a |
|
Unescapes any EcmaScript literals found in the |
|
Unescapes a string containing entity escapes to a string containing the actual Unicode characters corresponding to the escapes. |
|
Unescapes a string containing entity escapes to a string containing the actual Unicode characters corresponding to the escapes. |
|
Unescapes any Java literals found in the |
|
Unescapes any Json literals found in the |
|
Unescapes a string containing XML entity escapes to a string containing the actual Unicode characters corresponding to the escapes. |
- public static final String escapeCsv(String input)[source]¶
Returns a
Stringvalue for a CSV column enclosed in double quotes, if required.If the value contains a comma, newline or double quote, then the String value is returned enclosed in double quotes.
Any double quote characters in the value are escaped with another double quote.
If the value does not contain a comma, newline or double quote, then the String value is returned unchanged.
- Parameters:
input – The input CSV column String, may be null
- Returns:
The input String, enclosed in double quotes if the value contains a comma, newline or double quote,
nullif null string input- Since:
2.4
- public static final String escapeEcmaScript(String input)[source]¶
Escapes the characters in a
Stringusing EcmaScript String rules.Escapes any values it finds into their EcmaScript String form. Escapes the EcmaScript string delimiters (single quote, double quote and (since ES6) the backtick) as well as the template-literal interpolation sequence
${and control-chars (tab, backslash, cr, ff, etc.).So a tab becomes the characters
'\\'and't'.The differences between Java strings and EcmaScript strings handled here are that in EcmaScript, a single quote, the backtick, the
${sequence and forward-slash (/) are escaped.Scope: the output is a correctly escaped EcmaScript string literal for any of the three string delimiters, but it is not made safe for direct embedding inside an HTML inline
<script>block: HTML parser-state sequences such as<!--and<scriptpass through unchanged (a literal</script>is neutralized by the forward-slash escape). HTML-context encoding must be applied separately when the result is placed in HTML.Note that EcmaScript is best known by the JavaScript and ActionScript dialects.
Example:
input string: He didn't say, "Stop!" output string: He didn\'t say, \"Stop!\"
- Parameters:
input – String to escape values in, may be null
- Returns:
String with escaped values,
nullif null string input- Since:
3.0
- public static final String escapeHtml3(String input)[source]¶
Escapes the characters in a
Stringusing HTML entities.Supports only the HTML 3.0 entities. Apostrophes are escaped as the numeric reference
'.HTML attribute values must be quoted. This method does not escape all characters that delimit unquoted attribute values.
- public static final String escapeHtml4(String input)[source]¶
Escapes the characters in a
Stringusing HTML entities.For example:
"bread" & "butter"becomes:
&quot;bread&quot; &amp; &quot;butter&quot;.Supports all known HTML 4.0 entities, including funky accents. Apostrophes are escaped as the numeric reference
'because'is not a legal HTML 4.0 entity.HTML attribute values must be quoted. This method does not escape all characters that delimit unquoted attribute values.
- public static final String escapeJava(String input)[source]¶
Escapes the characters in a
Stringusing 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!\"
- Parameters:
input – String to escape values in, may be null
- Returns:
String with escaped values,
nullif null string input
- public static final String escapeJson(String input)[source]¶
Escapes the characters in a
Stringusing Json String rules.Escapes any values it finds into their JSON String form. 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 Json strings is that in Json, forward-slash (/) is escaped.
Scope: the output is a correctly escaped JSON string, but it is not made safe for direct embedding inside an HTML inline
<script>block: the backtick,${, and HTML parser-state sequences such as<!--and<scriptpass through unchanged (JSON offers no backslash escape for them; only a literal</script>is neutralized by the forward-slash escape). HTML-context encoding must be applied separately when the result is placed in HTML.See https://www.ietf.org/rfc/rfc4627.txt for further details.
Example:
input string: He didn't say, "Stop!" output string: He didn't say, \"Stop!\"
- Parameters:
input – String to escape values in, may be null
- Returns:
String with escaped values,
nullif null string input- Since:
3.2
- @Deprecated public static final String escapeXml(String input)[source]¶
Deprecated: Use
#escapeXml10(java.lang.String)or#escapeXml11(java.lang.String)instead.Escapes the characters in a
Stringusing XML entities.For example:
"bread" & "butter"=>"bread" & "butter".Supports only the five basic XML entities (gt, lt, quot, amp, apos). Does not support DTDs or external entities.
Note that Unicode characters greater than 0x7f are as of 3.0, no longer escaped. If you still wish this functionality, you can achieve it via the following:
StringEscapeUtils.ESCAPE_XML.with( NumericEntityEscaper.between(0x7f, Integer.MAX_VALUE));
- public static String escapeXml10(String input)[source]¶
Escapes the characters in a
Stringusing XML entities.For example:
"bread" & "butter"
converts to:
"bread" & "butter"
Note that XML 1.0 is a text-only format: it cannot represent control characters or unpaired Unicode surrogate code points, even after escaping. The method
escapeXml10will remove characters that do not fit in the following ranges:#x9 | #xA | #xD | [#x20-#xD7FF] | [#xE000-#xFFFD] | [#x10000-#x10FFFF]Though not strictly necessary,
escapeXml10will escape characters in the following ranges:[#x7F-#x84] | [#x86-#x9F]The returned string can be inserted into a valid XML 1.0 or XML 1.1 document. If you want to allow more non-text characters in an XML 1.1 document, use
#escapeXml11(String).
- public static String escapeXml11(String input)[source]¶
Escapes the characters in a
Stringusing XML entities.For example:
"bread" & "butter"=>"bread" & "butter".XML 1.1 can represent certain control characters, but it cannot represent the null byte or unpaired Unicode surrogate code points, even after escaping.
escapeXml11will remove characters that do not fit in the following ranges:[#x1-#xD7FF] | [#xE000-#xFFFD] | [#x10000-#x10FFFF]escapeXml11will escape characters in the following ranges:[#x1-#x8] | [#xB-#xC] | [#xE-#x1F] | [#x7F-#x84] | [#x86-#x9F]The returned string can be inserted into a valid XML 1.1 document. Do not use it for XML 1.0 documents.
- public static final String unescapeCsv(String input)[source]¶
Returns a
Stringvalue for an unescaped CSV column.If the value is enclosed in double quotes, and contains a comma, newline or double quote, then quotes are removed.
Any double quote escaped characters (a pair of double quotes) are unescaped to just one double quote.
If the value is not enclosed in double quotes, or is and does not contain a comma, newline or double quote, then the String value is returned unchanged.
- Parameters:
input – The input CSV column String, may be null
- Returns:
The input String, with enclosing double quotes removed and embedded double quotes unescaped,
nullif null string input- Since:
2.4
- public static final String unescapeEcmaScript(String input)[source]¶
Unescapes any EcmaScript literals found in the
String.For example, it will turn a sequence of
'\'and'n'into a newline character, unless the'\'is preceded by another'\'.
- public static final String unescapeHtml3(String input)[source]¶
Unescapes a string containing entity escapes to a string containing the actual Unicode characters corresponding to the escapes. Supports only HTML 3.0 entities.
- public static final String unescapeHtml4(String input)[source]¶
Unescapes a string containing entity escapes to a string containing the actual Unicode characters corresponding to the escapes. Supports HTML 4.0 entities.
For example, the string
"<Français>"will become"<Français>"If an entity is unrecognized, it is left alone, and inserted verbatim into the result string. e.g.
">&zzzz;x"will become">&zzzz;x".
- public static final String unescapeJava(String input)[source]¶
Unescapes any Java literals found in the
String. For example, it will turn a sequence of'\'and'n'into a newline character, unless the'\'is preceded by another'\'.
- public static final String unescapeJson(String input)[source]¶
Unescapes any Json literals found in the
String.For example, it will turn a sequence of
'\'and'n'into a newline character, unless the'\'is preceded by another'\'.
- public static final String unescapeXml(String input)[source]¶
Unescapes a string containing XML entity escapes to a string containing the actual Unicode characters corresponding to the escapes.
Supports only the five basic XML entities (gt, lt, quot, amp, apos). Does not support DTDs or external entities.
Note that numerical \u Unicode codes are unescaped to their respective Unicode characters. This may change in future releases.