Here you can find the source of unescape(String string)
Parameter | Description |
---|---|
string | The string to unescape. |
public static final String unescape(String string)
//package com.java2s; public class Main { /**//from ww w .ja v a 2s . c o m * This method unescapes special characters in the given string. * @param string The string to unescape. * @return A string with special characters unescaped. */ public static final String unescape(String string) { StringBuilder sb = new StringBuilder(); boolean wasEscape = false; boolean unicode = false; int count = 0; for (char c : string.toCharArray()) { if (unicode) { sb.append(c); if (++count == 4) { int s = sb.length() - 4; String code = sb.substring(s); sb.setLength(s); try { int ncode = Integer.parseInt(code, 16); sb.append((char) ncode); } catch (NumberFormatException e) { sb.append("\\u"); sb.append(code); } unicode = false; } } else if (wasEscape) { switch (c) { case '0': sb.append('\0'); break; case 'b': sb.append('\b'); break; case 'f': sb.append('\f'); break; case 't': sb.append('\t'); break; case 'r': sb.append('\r'); break; case 'n': sb.append('\n'); break; case '\\': sb.append('\\'); break; case '\'': sb.append('\''); break; case '"': sb.append('"'); break; case 'u': unicode = true; count = 0; break; default: sb.append('\\'); sb.append(c); } wasEscape = false; } else if (c == '\\') { wasEscape = true; } else { sb.append(c); } } if (unicode) { int s = sb.length() - count; String code = sb.substring(s); sb.setLength(s); sb.append("\\u"); sb.append(code); } if (wasEscape) sb.append('\\'); return sb.toString(); } }