Java String Unescape unescape(String string)

Here you can find the source of unescape(String string)

Description

This method unescapes special characters in the given string.

License

Open Source License

Parameter

Parameter Description
string The string to unescape.

Return

A string with special characters unescaped.

Declaration

public static final String unescape(String string) 

Method Source Code

//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();
    }
}

Related

  1. unescape(String str)
  2. unescape(String str)
  3. unescape(String str)
  4. unescape(String str, char escapeChar, char[] targetChars, char[] escapedChars)
  5. unescape(String string)
  6. unescape(String string)
  7. unescape(String string)
  8. unescape(String string)
  9. unescape(String string, int escape)