public class StringEscapeUtils extends Object
Adapted from Apache Commons code
<p>Escapes and unescapes <code>String</code>s for Java, Java Script, HTML, XML, and SQL.</p> <p></p> <p>#ThreadSafe#</p>
Constructor and Description |
---|
StringEscapeUtils()
<p><code>StringEscapeUtils</code> instances should NOT be constructed in
standard programming.</p>
<p></p>
<p>Instead, the class should be used as:</p>
<pre>StringEscapeUtils.escapeJava("foo");</pre>
<p></p>
<p>This constructor is public to permit tools that require a JavaBean
instance to operate.</p>
|
Modifier and Type | Method and Description |
---|---|
static String |
escapeJava(String str)
<p>Escapes the characters in a <code>String</code> using Java String rules.</p>
<p></p>
<p>Deals correctly with quotes and control-chars (tab, backslash, cr, ff, etc.) </p>
<p></p>
<p>So a tab becomes the characters <code>'\\'</code> and
<code>'t'</code>.</p>
<p></p>
<p>The only difference between Java strings and JavaScript strings
is that in JavaScript, a single quote must be escaped.</p>
<p></p>
<p>Example:</p>
<pre>
input string: He didn’t say, "Stop!"
output string: He didn’t say, \"Stop!\"
</pre>
|
static void |
escapeJava(Writer out,
String str)
<p>Escapes the characters in a <code>String</code> using Java String rules to
a <code>Writer</code>.</p>
<p></p>
<p>A <code>null</code> string input has no effect.</p>
|
static String |
escapeJavaScript(String str)
<p>Escapes the characters in a <code>String</code> using JavaScript String rules.</p>
<p>Escapes any values it finds into their JavaScript String form.
|
static void |
escapeJavaScript(Writer out,
String str)
<p>Escapes the characters in a <code>String</code> using JavaScript String rules
to a <code>Writer</code>.</p>
<p></p>
<p>A <code>null</code> string input has no effect.</p>
|
static String |
unescapeJava(String str)
<p>Unescapes any Java literals found in the <code>String</code>.
|
static void |
unescapeJava(Writer out,
String str)
<p>Unescapes any Java literals found in the <code>String</code> to a
<code>Writer</code>.</p>
<p></p>
<p>For example, it will turn a sequence of <code>'\'</code> and
<code>'n'</code> into a newline character, unless the <code>'\'</code>
is preceded by another <code>'\'</code>.</p>
<p></p>
<p>A <code>null</code> string input has no effect.</p>
|
static String |
unescapeJavaScript(String str)
<p>Unescapes any JavaScript literals found in the <code>String</code>.</p>
<p></p>
<p>For example, it will turn a sequence of <code>'\'</code> and <code>'n'</code>
into a newline character, unless the <code>'\'</code> is preceded by another
<code>'\'</code>.</p>
|
static void |
unescapeJavaScript(Writer out,
String str)
<p>Unescapes any JavaScript literals found in the <code>String</code> to a
<code>Writer</code>.</p>
<p></p>
<p>For example, it will turn a sequence of <code>'\'</code> and <code>'n'</code>
into a newline character, unless the <code>'\'</code> is preceded by another
<code>'\'</code>.</p>
<p></p>
<p>A <code>null</code> string input has no effect.</p>
|
public StringEscapeUtils()
<p><code>StringEscapeUtils</code> instances should NOT be constructed in standard programming.</p> <p></p> <p>Instead, the class should be used as:</p> <pre>StringEscapeUtils.escapeJava("foo");</pre> <p></p> <p>This constructor is public to permit tools that require a JavaBean instance to operate.</p>
public static String escapeJava(String str) throws Exception
<p>Escapes the characters in a <code>String</code> using Java String rules.</p> <p></p> <p>Deals correctly with quotes and control-chars (tab, backslash, cr, ff, etc.) </p> <p></p> <p>So a tab becomes the characters <code>'\\'</code> and <code>'t'</code>.</p> <p></p> <p>The only difference between Java strings and JavaScript strings is that in JavaScript, a single quote must be escaped.</p> <p></p> <p>Example:</p> <pre> input string: He didn’t say, "Stop!" output string: He didn’t say, \"Stop!\" </pre>
str
- String to escape values in, may be nullException
public static void escapeJava(Writer out, String str) throws IOException
<p>Escapes the characters in a <code>String</code> using Java String rules to a <code>Writer</code>.</p> <p></p> <p>A <code>null</code> string input has no effect.</p>
out
- Writer to write escaped string intostr
- String to escape values in, may be nullIllegalArgumentException
- if the Writer is <code>null</code>IOException
- if error occurs on underlying WriterescapeJava(String)
public static String escapeJavaScript(String str) throws Exception
<p>Escapes the characters in a <code>String</code> using JavaScript String rules.</p> <p>Escapes any values it finds into their JavaScript String form. Deals correctly with quotes and control-chars (tab, backslash, cr, ff, etc.) </p> <p></p> <p>So a tab becomes the characters <code>'\\'</code> and <code>'t'</code>.</p> <p></p> <p>The only difference between Java strings and JavaScript strings is that in JavaScript, a single quote must be escaped.</p> <p></p> <p>Example:</p> <pre> input string: He didn’t say, "Stop!" output string: He didn't say, \"Stop!\" </pre>
str
- String to escape values in, may be nullException
public static void escapeJavaScript(Writer out, String str) throws Exception
<p>Escapes the characters in a <code>String</code> using JavaScript String rules to a <code>Writer</code>.</p> <p></p> <p>A <code>null</code> string input has no effect.</p>
out
- Writer to write escaped string intostr
- String to escape values in, may be nullIllegalArgumentException
- if the Writer is <code>null</code>IOException
- if error occurs on underlying WriterException
escapeJavaScript(String)
public static String unescapeJava(String str) throws Exception
<p>Unescapes any Java literals found in the <code>String</code>. For example, it will turn a sequence of <code>'\'</code> and <code>'n'</code> into a newline character, unless the <code>'\'</code> is preceded by another <code>'\'</code>.</p>
str
- the <code>String</code> to unescape, may be nullException
public static void unescapeJava(Writer out, String str) throws Exception
<p>Unescapes any Java literals found in the <code>String</code> to a <code>Writer</code>.</p> <p></p> <p>For example, it will turn a sequence of <code>'\'</code> and <code>'n'</code> into a newline character, unless the <code>'\'</code> is preceded by another <code>'\'</code>.</p> <p></p> <p>A <code>null</code> string input has no effect.</p>
out
- the <code>Writer</code> used to output unescaped charactersstr
- the <code>String</code> to unescape, may be nullIllegalArgumentException
- if the Writer is <code>null</code>IOException
- if error occurs on underlying WriterException
public static String unescapeJavaScript(String str) throws Exception
<p>Unescapes any JavaScript literals found in the <code>String</code>.</p> <p></p> <p>For example, it will turn a sequence of <code>'\'</code> and <code>'n'</code> into a newline character, unless the <code>'\'</code> is preceded by another <code>'\'</code>.</p>
str
- the <code>String</code> to unescape, may be nullException
unescapeJava(String)
public static void unescapeJavaScript(Writer out, String str) throws Exception
<p>Unescapes any JavaScript literals found in the <code>String</code> to a <code>Writer</code>.</p> <p></p> <p>For example, it will turn a sequence of <code>'\'</code> and <code>'n'</code> into a newline character, unless the <code>'\'</code> is preceded by another <code>'\'</code>.</p> <p></p> <p>A <code>null</code> string input has no effect.</p>
out
- the <code>Writer</code> used to output unescaped charactersstr
- the <code>String</code> to unescape, may be nullIllegalArgumentException
- if the Writer is <code>null</code>IOException
- if error occurs on underlying WriterException
unescapeJava(java.io.Writer, String)
Copyright © 2014. All Rights Reserved.