Java String Unescape unescape(String str)

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

Description

Unescape special characters in literal by removing excess backslashes.

License

Open Source License

Parameter

Parameter Description
str The string to escape

Declaration

public static String unescape(String str) 

Method Source Code

//package com.java2s;
//License from project: Open Source License 

public class Main {
    /**/*from ww w . j  a v  a  2  s.  c om*/
     * Unescape special characters in literal by removing excess backslashes.
     * 
     * @param str
     *            The string to escape
     */
    public static String unescape(String str) {
        return unescape(str, false);
    }

    /**
     * Unescape special characters in literal by removing excess backslashes.
     * 
     * @param str
     *            The string to escape
     * @param clean
     *            If true, cleans up excess slashes
     */
    public static String unescape(String str, boolean clean) {
        if (clean)
            str = cleanSlashes(str);
        int sz = str.length();

        StringBuffer buffer = new StringBuffer(sz);
        StringBuffer unicode = new StringBuffer(6);

        boolean hadSlash = false;
        boolean inUnicode = false;
        boolean inSpecialUnicode = false;

        for (int i = 0; i < sz; i++) {
            char ch = str.charAt(i);
            if (inUnicode) {
                // if in unicode, then we're reading unicode
                // values in somehow

                if (unicode.length() < 4) {
                    unicode.append(ch);

                    if (unicode.length() == 4) {
                        // unicode now contains the four hex digits
                        // which represents our unicode chacater
                        try {
                            int value = Integer.parseInt(
                                    unicode.toString(), 16);
                            buffer.append((char) value);
                            unicode = new StringBuffer(4);
                            inUnicode = false;
                            inSpecialUnicode = false;
                            hadSlash = false;
                        } catch (NumberFormatException nfe) {
                            buffer.append(unicode.toString());
                            continue;
                        }
                        continue;
                    }
                    continue;
                }

            }

            if (inSpecialUnicode) {
                // if in unicode, then we're reading unicode
                // values in somehow

                if (unicode.length() < 8) {
                    unicode.append(ch);

                    if (unicode.length() == 8) {
                        // unicode now contains the six hex digits
                        // which represents our code point
                        try {
                            buffer.appendCodePoint(Integer.parseInt(
                                    unicode.toString(), 16));

                            unicode = new StringBuffer(8);
                            inUnicode = false;
                            inSpecialUnicode = false;
                            hadSlash = false;
                        } catch (NumberFormatException nfe) {
                            buffer.append(unicode.toString());
                            continue;
                        }
                        continue;
                    }
                    continue;
                }

            }

            if (hadSlash) {
                // handle an escaped value
                hadSlash = false;
                switch (ch) {
                case '\\':
                    buffer.append('\\');
                    break;
                case '\'':
                    buffer.append('\'');
                    break;
                case '\"':
                    buffer.append('"');
                    break;
                case 'r':
                    buffer.append('\r');
                    break;
                case 'f':
                    buffer.append('\f');
                    break;
                case 't':
                    buffer.append('\t');
                    break;
                case 'n':
                    buffer.append('\n');
                    break;
                case 'b':
                    buffer.append('\b');
                    break;
                case 'u': {
                    // uh-oh, we're in unicode country....
                    inUnicode = true;
                    break;

                }
                case 'U': {
                    // even more uh-oh, we're in special unicode land...
                    inSpecialUnicode = true;
                    break;
                }

                default:
                    buffer.append(ch);
                    break;
                }
                continue;
            }

            else if (ch == '\\') {
                hadSlash = true;
                continue;
            }

            buffer.append(ch);
        }

        if (hadSlash) {
            // then we're in the weird case of a \ at the end of the
            // string, let's output it anyway.
            buffer.append('\\');
        }
        return buffer.toString();
    }

    /**
     * Remove multiples of \\ for cleaning data escaped multiple times.
     */
    private static String cleanSlashes(String str) {
        while (str.indexOf("\\\\") != -1)
            str = str.replaceAll("\\\\\\\\", "\\\\");
        return str;
    }
}

Related

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