Java String Unescape unescape(String string)

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

Description

Returns the specified string with Java escape sequences (that is '\n', '\u00E9', etc) replaced by the corresponding character.

License

Open Source License

Parameter

Parameter Description
string the String to be unescaped

Return

the specified string with Java escape sequences replaced by the corresponding character

Declaration

public static String unescape(String string) 

Method Source Code

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

Related

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