Class StringEscapeUtils
Escapes and unescapes Strings for
Java, Java Script, HTML, XML, and SQL.
#ThreadSafe#
- Since:
- 2.0
- Version:
- $Id: StringEscapeUtils.java 1057072 2011-01-10 01:55:57Z niallp $
-
Constructor Summary
ConstructorsConstructorDescriptionStringEscapeUtilsinstances should NOT be constructed in standard programming. -
Method Summary
Modifier and TypeMethodDescriptionstatic voidWrites aStringvalue for a CSV column enclosed in double quotes, if required.static StringReturns aStringvalue for a CSV column enclosed in double quotes, if required.static voidescapeHtml(Writer writer, String string) Escapes the characters in aStringusing HTML entities and writes them to aWriter.static StringescapeHtml(String str) Escapes the characters in aStringusing HTML entities.static voidescapeJava(Writer out, String str) Escapes the characters in aStringusing Java String rules to aWriter.static StringescapeJava(String str) Escapes the characters in aStringusing Java String rules.static voidescapeJavaScript(Writer out, String str) Escapes the characters in aStringusing JavaScript String rules to aWriter.static StringescapeJavaScript(String str) Escapes the characters in aStringusing JavaScript String rules.static StringEscapes the characters in aStringto be suitable to pass to an SQL query.static voidEscapes the characters in aStringusing XML entities.static StringEscapes the characters in aStringusing XML entities.static voidunescapeCsv(Writer out, String str) Returns aStringvalue for an unescaped CSV column.static StringunescapeCsv(String str) Returns aStringvalue for an unescaped CSV column.static voidunescapeHtml(Writer writer, String string) Unescapes a string containing entity escapes to a string containing the actual Unicode characters corresponding to the escapes.static StringunescapeHtml(String str) Unescapes a string containing entity escapes to a string containing the actual Unicode characters corresponding to the escapes.static voidunescapeJava(Writer out, String str) Unescapes any Java literals found in theStringto aWriter.static StringunescapeJava(String str) Unescapes any Java literals found in theString.static voidunescapeJavaScript(Writer out, String str) Unescapes any JavaScript literals found in theStringto aWriter.static StringunescapeJavaScript(String str) Unescapes any JavaScript literals found in theString.static voidunescapeXml(Writer writer, String str) Unescapes a string containing XML entity escapes to a string containing the actual Unicode characters corresponding to the escapes.static StringunescapeXml(String str) Unescapes a string containing XML entity escapes to a string containing the actual Unicode characters corresponding to the escapes.
-
Constructor Details
-
StringEscapeUtils
public StringEscapeUtils()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.
-
-
Method Details
-
escapeJava
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 must be escaped.
Example:
input string: He didn't say, "Stop!" output string: He didn't say, \"Stop!\"
- Parameters:
str- String to escape values in, may be null- Returns:
- String with escaped values,
nullif null string input
-
escapeJava
Escapes the characters in a
Stringusing Java String rules to aWriter.A
nullstring input has no effect.- Parameters:
out- Writer to write escaped string intostr- String to escape values in, may be null- Throws:
IllegalArgumentException- if the Writer isnullIOException- if error occurs on underlying Writer- See Also:
-
escapeJavaScript
Escapes the characters in a
Stringusing JavaScript String rules.Escapes any values it finds into their JavaScript 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 JavaScript strings is that in JavaScript, a single quote must be escaped.
Example:
input string: He didn't say, "Stop!" output string: He didn\'t say, \"Stop!\"
- Parameters:
str- String to escape values in, may be null- Returns:
- String with escaped values,
nullif null string input
-
escapeJavaScript
Escapes the characters in a
Stringusing JavaScript String rules to aWriter.A
nullstring input has no effect.- Parameters:
out- Writer to write escaped string intostr- String to escape values in, may be null- Throws:
IllegalArgumentException- if the Writer isnullIOException- if error occurs on underlying Writer- See Also:
-
unescapeJava
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'\'.- Parameters:
str- theStringto unescape, may be null- Returns:
- a new unescaped
String,nullif null string input
-
unescapeJava
Unescapes any Java literals found in the
Stringto aWriter.For example, it will turn a sequence of
'\'and'n'into a newline character, unless the'\'is preceded by another'\'.A
nullstring input has no effect.- Parameters:
out- theWriterused to output unescaped charactersstr- theStringto unescape, may be null- Throws:
IllegalArgumentException- if the Writer isnullIOException- if error occurs on underlying Writer
-
unescapeJavaScript
Unescapes any JavaScript 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'\'.- Parameters:
str- theStringto unescape, may be null- Returns:
- A new unescaped
String,nullif null string input - See Also:
-
unescapeJavaScript
Unescapes any JavaScript literals found in the
Stringto aWriter.For example, it will turn a sequence of
'\'and'n'into a newline character, unless the'\'is preceded by another'\'.A
nullstring input has no effect.- Parameters:
out- theWriterused to output unescaped charactersstr- theStringto unescape, may be null- Throws:
IllegalArgumentException- if the Writer isnullIOException- if error occurs on underlying Writer- See Also:
-
escapeHtml
Escapes the characters in a
Stringusing HTML entities.For example:
becomes:"bread" invalid input: '&' "butter""bread" & "butter".Supports all known HTML 4.0 entities, including funky accents. Note that the commonly used apostrophe escape character (') is not a legal entity and so is not supported).
- Parameters:
str- theStringto escape, may be null- Returns:
- a new escaped
String,nullif null string input - See Also:
-
escapeHtml
Escapes the characters in a
Stringusing HTML entities and writes them to aWriter.For example:
"bread" invalid input: '&' "butter"becomes:
"bread" & "butter".Supports all known HTML 4.0 entities, including funky accents. Note that the commonly used apostrophe escape character (') is not a legal entity and so is not supported).
- Parameters:
writer- the writer receiving the escaped string, not nullstring- theStringto escape, may be null- Throws:
IllegalArgumentException- if the writer is nullIOException- whenWriterpassed throws the exception from calls to theWriter.write(int)methods.- See Also:
-
unescapeHtml
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".
- Parameters:
str- theStringto unescape, may be null- Returns:
- a new unescaped
String,nullif null string input - See Also:
-
unescapeHtml
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".
- Parameters:
writer- the writer receiving the unescaped string, not nullstring- theStringto unescape, may be null- Throws:
IllegalArgumentException- if the writer is nullIOException- if an IOException occurs- See Also:
-
escapeXml
Escapes the characters in a
Stringusing XML entities.For example: "bread" invalid input: '&' "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 currently escaped to their numerical \\u equivalent. This may change in future releases.
- Parameters:
writer- the writer receiving the unescaped string, not nullstr- theStringto escape, may be null- Throws:
IllegalArgumentException- if the writer is nullIOException- if there is a problem writing- See Also:
-
escapeXml
Escapes the characters in a
Stringusing XML entities.For example: "bread" invalid input: '&' "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 currently escaped to their numerical \\u equivalent. This may change in future releases.
- Parameters:
str- theStringto escape, may be null- Returns:
- a new escaped
String,nullif null string input - See Also:
-
unescapeXml
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.
- Parameters:
writer- the writer receiving the unescaped string, not nullstr- theStringto unescape, may be null- Throws:
IllegalArgumentException- if the writer is nullIOException- if there is a problem writing- See Also:
-
unescapeXml
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.
- Parameters:
str- theStringto unescape, may be null- Returns:
- a new unescaped
String,nullif null string input - See Also:
-
escapeSql
Escapes the characters in a
Stringto be suitable to pass to an SQL query.For example,
statement.executeQuery("SELECT * FROM MOVIES WHERE TITLE='" + StringEscapeUtils.escapeSql("McHale's Navy") + "'");At present, this method only turns single-quotes into doubled single-quotes (
see http://www.jguru.com/faq/view.jsp?EID=8881"McHale's Navy"=>"McHale''s Navy"). It does not handle the cases of percent (%) or underscore (_) for use in LIKE clauses.- Parameters:
str- the string to escape, may be null- Returns:
- a new String, escaped for SQL,
nullif null string input
-
escapeCsv
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.
see Wikipedia and RFC 4180.- Parameters:
str- 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
-
escapeCsv
Writes 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 written 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 written unchanged (null values are ignored).
see Wikipedia and RFC 4180.- Parameters:
out- Writer to write input string to, enclosed in double quotes if it contains a comma, newline or double quotestr- the input CSV column String, may be null- Throws:
IOException- if error occurs on underlying Writer- Since:
- 2.4
-
unescapeCsv
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.
see Wikipedia and RFC 4180.- Parameters:
str- 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
-
unescapeCsv
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.
see Wikipedia and RFC 4180.- Parameters:
out- Writer to write the input String to, with enclosing double quotes removed and embedded double quotes unescaped,nullif null string inputstr- the input CSV column String, may be null- Throws:
IOException- if error occurs on underlying Writer- Since:
- 2.4
-