Summary
This feature introduces a new String instance method to translate escape sequences to characters.
Problem
Raw String Literals do not interpret Unicode escapes (\unnnn
) or escape
sequences ( \n
, \b
, etc). A solution is required to handle scenarios when
the developer wants multi-line strings and still have escapes translated.
Solution
Introduce a String instance method, unescape
which translates escape
sequences to characters.
Specification
/**
* Translates all Unicode escapes and escape sequences in this string into
* characters represented by those escapes specified in sections 3.3 and
* 3.10.6 of the <cite>The Java™ Language Specification</cite>.
* <p>
* Backslash escape sequences are translated as follows;
* <table class="plain">
* <caption style="display:none">Escape sequences</caption>
* <thead>
* <tr>
* <th scope="col">Escape</th>
* <th scope="col">Name</th>
* <th scope="col">Unicode</th>
* </tr>
* </thead>
* <tr>
* <td>{@code \b}</td>
* <td>backspace</td>
* <td>U+0008</td>
* </tr>
* <tr>
* <td>{@code \t}</td>
* <td>horizontal tab</td>
* <td>U+0009</td>
* </tr>
* <tr>
* <td>{@code \n}</td>
* <td>line feed</td>
* <td>U+000A</td>
* </tr>
* <tr>
* <td>{@code \f}</td>
* <td>form feed</td>
* <td>U+000C</td>
* </tr>
* <tr>
* <td>{@code \r}</td>
* <td>carriage return</td>
* <td>U+000D</td>
* </tr>
* <tr>
* <td>{@code \"}</td>
* <td>double quote</td>
* <td>U+0022</td>
* </tr>
* <tr>
* <td>{@code \'}</td>
* <td>single quote</td>
* <td>U+0027</td>
* </tr>
* <tr>
* <td>{@code \\}</td>
* <td>backslash</td>
* <td>U+005C</td>
* </tr>
* </table>
* <p>
* Octal escapes {@code \0 - \377} are translated to their code
* point equivalents.
* <p>
* Each unicode escape in the form \unnnn is translated to a
* 16-bit 'char' value, which may then be translated as part
* of backslash escape sequence. For example, "\u005ct" becomes
* "\t", which then gets translated to tab (U+0009) character.
*
* @return String with all escapes translated.
*
* @throws IllegalArgumentException when the escape sequence does
* not conform to JLS 3.3 or 3.10.6.
*
* @jls 3.3 Unicode escapes
* @jls 3.10.6 Escape sequences
*
* @since 12
*
* @deprecated Preview feature associated with Raw String Literals.
*/
@Deprecated(forRemoval=true, since="12")
public String unescape() {
- csr of
-
JDK-8202442 String::unescape
-
- Closed
-