Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    primarykey
    data
    text
    <p>You can use <a href="http://commons.apache.org/proper/commons-lang/javadocs/api-release/org/apache/commons/lang3/StringEscapeUtils.html#unescapeJava(java.lang.String)" rel="noreferrer"><code>String unescapeJava(String)</code></a> method of <a href="http://commons.apache.org/proper/commons-lang/javadocs/api-release/org/apache/commons/lang3/StringEscapeUtils.html" rel="noreferrer"><code>StringEscapeUtils</code></a> from <a href="http://commons.apache.org/lang/" rel="noreferrer">Apache Commons Lang</a>.</p> <p>Here's an example snippet:</p> <pre><code> String in = "a\\tb\\n\\\"c\\\""; System.out.println(in); // a\tb\n\"c\" String out = StringEscapeUtils.unescapeJava(in); System.out.println(out); // a b // "c" </code></pre> <p>The utility class has methods to escapes and unescape strings for Java, Java Script, HTML, XML, and SQL. It also has overloads that writes directly to a <a href="http://java.sun.com/javase/6/docs/api/java/io/Writer.html" rel="noreferrer"><code>java.io.Writer</code></a>.</p> <hr> <h3>Caveats</h3> <p>It looks like <code>StringEscapeUtils</code> handles Unicode escapes with one <code>u</code>, but not octal escapes, or Unicode escapes with extraneous <code>u</code>s.</p> <pre><code> /* Unicode escape test #1: PASS */ System.out.println( "\u0030" ); // 0 System.out.println( StringEscapeUtils.unescapeJava("\\u0030") ); // 0 System.out.println( "\u0030".equals(StringEscapeUtils.unescapeJava("\\u0030")) ); // true /* Octal escape test: FAIL */ System.out.println( "\45" ); // % System.out.println( StringEscapeUtils.unescapeJava("\\45") ); // 45 System.out.println( "\45".equals(StringEscapeUtils.unescapeJava("\\45")) ); // false /* Unicode escape test #2: FAIL */ System.out.println( "\uu0030" ); // 0 System.out.println( StringEscapeUtils.unescapeJava("\\uu0030") ); // throws NestableRuntimeException: // Unable to parse unicode value: u003 </code></pre> <p>A quote from the JLS:</p> <blockquote> <p>Octal escapes are provided for compatibility with C, but can express only Unicode values <code>\u0000</code> through <code>\u00FF</code>, so Unicode escapes are usually preferred.</p> </blockquote> <p>If your string can contain octal escapes, you may want to convert them to Unicode escapes first, or use another approach.</p> <p>The extraneous <code>u</code> is also documented as follows:</p> <blockquote> <p>The Java programming language specifies a standard way of transforming a program written in Unicode into ASCII that changes a program into a form that can be processed by ASCII-based tools. The transformation involves converting any Unicode escapes in the source text of the program to ASCII by adding an extra <code>u</code>-for example, <code>\uxxxx</code> becomes <code>\uuxxxx</code>-while simultaneously converting non-ASCII characters in the source text to Unicode escapes containing a single u each.</p> <p>This transformed version is equally acceptable to a compiler for the Java programming language and represents the exact same program. The exact Unicode source can later be restored from this ASCII form by converting each escape sequence where multiple <code>u</code>'s are present to a sequence of Unicode characters with one fewer <code>u</code>, while simultaneously converting each escape sequence with a single <code>u</code> to the corresponding single Unicode character.</p> </blockquote> <p>If your string can contain Unicode escapes with extraneous <code>u</code>, then you may also need to preprocess this before using <code>StringEscapeUtils</code>.</p> <p>Alternatively you can try to write your own Java string literal unescaper from scratch, making sure to follow the exact JLS specifications.</p> <h3>References</h3> <ul> <li><a href="http://java.sun.com/docs/books/jls/third_edition/html/lexical.html#3.3" rel="noreferrer">JLS 3.3 Unicode Escapes</a></li> <li><a href="http://java.sun.com/docs/books/jls/third_edition/html/lexical.html#3.10.6" rel="noreferrer">JLS 3.10.6 Escape Sequences for Character and String Literals</a></li> </ul>
    singulars
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. VO
      singulars
      1. This table or related slice is empty.
    2. VO
      singulars
      1. This table or related slice is empty.
    3. VO
      singulars
      1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload