Here you can find the source of unescape(String oldStr)
public static String unescape(String oldStr) throws IllegalArgumentException
//package com.java2s; //License from project: Open Source License public class Main { public static String unescape(String oldStr) throws IllegalArgumentException { StringBuilder newStr = new StringBuilder(oldStr.length()); boolean backSlashPresent = false; for (int i = 0; i < oldStr.length(); i++) { int cp = oldStr.codePointAt(i); if (oldStr.codePointAt(i) > Character.MAX_VALUE) { i++;//from w ww . j a va 2 s . c o m } if (!backSlashPresent) { if (cp == '\\') { backSlashPresent = true; } else { newStr.append(Character.toChars(cp)); } continue; } if (cp == '\\') { backSlashPresent = false; newStr.append('\\'); newStr.append('\\'); continue; } switch (cp) { case 'r': newStr.append('\r'); break; case 'n': newStr.append('\n'); break; case 'f': newStr.append('\f'); break; case 'b': newStr.append("\\b"); break; case 't': newStr.append('\t'); break; case 'a': newStr.append('\007'); break; case 'e': newStr.append('\033'); break; //* A "control" character is what you get when you xor its //* codepoint with '@'==64. This only makes sense for ASCII, //* and may not yield a "control" character after all. //* Strange but true: "\c{" is ";", "\c}" is "=", etc. case 'c': { if (++i == oldStr.length()) { throw new IllegalArgumentException("trailing \\c"); } cp = oldStr.codePointAt(i); if (cp > 0x7f) { throw new IllegalArgumentException("expected ASCII after \\c"); } newStr.append(Character.toChars(cp ^ 64)); break; } case '8': case '9': throw new IllegalArgumentException("illegal octal digit"); //* may be 0 to 2 octal digits following this one //* so back up one for fallthrough to next case; //* unread this digit and fall through to next case. case '1': case '2': case '3': case '4': case '5': case '6': case '7': --i; //* Can have 0, 1, or 2 octal digits following a 0 //* this permits larger values than octal 377, up to octal 777. case '0': { if (i + 1 == oldStr.length()) { /* found \0 at end of string */ newStr.append(Character.toChars(0)); break; } i++; int digits = 0; int j; for (j = 0; j <= 2; j++) { if (i + j == oldStr.length()) { break;//for } /* safe because will unread surrogate */ int ch = oldStr.charAt(i + j); if (ch < '0' || ch > '7') { break;//for } digits++; } if (digits == 0) { --i; newStr.append('\0'); break; } int value; try { value = Integer.parseInt(oldStr.substring(i, i + digits), 8); } catch (NumberFormatException nfe) { throw new IllegalArgumentException("invalid octal value for \\0 escape"); } newStr.append(Character.toChars(value)); i += digits - 1; break; } /* end case '0' */ case 'x': { if (i + 2 > oldStr.length()) { throw new IllegalArgumentException("string too short for \\x escape"); } i++; boolean saw_brace = false; if (oldStr.charAt(i) == '{') { //ok to ignore surrogates here i++; saw_brace = true; } int j; for (j = 0; j < 8; j++) { if (!saw_brace && j == 2) { break; /* for */ } /* * ASCII test also catches surrogates */ int ch = oldStr.charAt(i + j); if (ch > 127) { throw new IllegalArgumentException("illegal non-ASCII hex digit in \\x escape"); } if (saw_brace && ch == '}') { break; } if (!((ch >= '0' && ch <= '9') || (ch >= 'a' && ch <= 'f') || (ch >= 'A' && ch <= 'F'))) { throw new IllegalArgumentException( String.format("illegal hex digit #%d '%c' in \\x", ch, ch)); } } if (j == 0) { throw new IllegalArgumentException("empty braces in \\x{} escape"); } int value; try { value = Integer.parseInt(oldStr.substring(i, i + j), 16); } catch (NumberFormatException nfe) { throw new IllegalArgumentException("invalid hex value for \\x escape"); } newStr.append(Character.toChars(value)); if (saw_brace) { j++; } i += j - 1; break; } case 'u': { if (i + 4 > oldStr.length()) { throw new IllegalArgumentException("string too short for \\u escape"); } i++; int j; for (j = 0; j < 4; j++) { /* this also handles the surrogate issue */ if (oldStr.charAt(i + j) > 127) { throw new IllegalArgumentException("illegal non-ASCII hex digit in \\u escape"); } } int value; try { value = Integer.parseInt(oldStr.substring(i, i + j), 16); } catch (NumberFormatException nfe) { throw new IllegalArgumentException("invalid hex value for \\u escape"); } newStr.append(Character.toChars(value)); i += j - 1; break; } case 'U': { if (i + 8 > oldStr.length()) { throw new IllegalArgumentException("string too short for \\U escape"); } i++; int j; for (j = 0; j < 8; j++) { //this also handles the surrogate issue if (oldStr.charAt(i + j) > 127) { throw new IllegalArgumentException("illegal non-ASCII hex digit in \\U escape"); } } int value; try { value = Integer.parseInt(oldStr.substring(i, i + j), 16); } catch (NumberFormatException nfe) { throw new IllegalArgumentException("invalid hex value for \\U escape"); } newStr.append(Character.toChars(value)); i += j - 1; break; } default: newStr.append('\\'); newStr.append(Character.toChars(cp)); break; } backSlashPresent = false; } //weird to leave one at the end if (backSlashPresent) { newStr.append('\\'); } return newStr.toString(); } }