Here you can find the source of unescape(String string)
Parameter | Description |
---|---|
string | the String to be unescaped |
public static String unescape(String string)
//package com.java2s; public class Main { /**// w ww .j a va 2s. co m * Returns the specified string with Java escape sequences (that is * <tt>'\n'</tt>, <tt>'\u00E9'</tt>, etc) replaced by the corresponding * character. * * @param string the String to be unescaped * @return the specified string with Java escape sequences replaced by the * corresponding character */ public static String unescape(String string) { return unescape(string, 0, string.length()); } @SuppressWarnings("fallthrough") private static String unescape(String string, int offset, int length) { StringBuilder buffer = new StringBuilder(); int end = offset + length; for (int i = offset; i < end; ++i) { char c = string.charAt(i); switch (c) { case '\\': if (i + 1 == end) { buffer.append(c); } else { switch (string.charAt(i + 1)) { case 'b': buffer.append('\b'); ++i; break; case 't': buffer.append('\t'); ++i; break; case 'n': buffer.append('\n'); ++i; break; case 'f': buffer.append('\f'); ++i; break; case 'r': buffer.append('\r'); ++i; break; case '\"': buffer.append('\"'); ++i; break; case '\'': buffer.append('\''); ++i; break; case '\\': buffer.append('\\'); ++i; break; case 'u': if (i + 5 < end) { int escaped = -1; try { escaped = Integer.parseInt(string.substring(i + 2, i + 6), 16); } catch (NumberFormatException ignored) { } if (escaped >= 0) { buffer.append((char) escaped); i += 5; break; } } /*FALLTHROUGH*/ default: buffer.append(c); } } break; default: buffer.append(c); } } return buffer.toString(); } }