Java String Unquote unquote(String string)

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

Description

Like #unescape but removes the double quote characters ('\"'), if any, before unescaping the string.

License

Open Source License

Declaration

public static String unquote(String string) 

Method Source Code

//package com.java2s;

public class Main {
    /**// w w  w  .  j  a v  a2  s  . c o  m
     * Like {@link #unescape} but removes the double quote characters 
     * (<tt>'\"'</tt>), if any, before unescaping the string.
     */
    public static String unquote(String string) {
        int length = string.length();

        if (length >= 2 && string.charAt(0) == '\"' && string.charAt(length - 1) == '\"') {
            return unescape(string, 1, length - 2);
        } else {
            return unescape(string, 0, length);
        }
    }

    /**
     * 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. unquote(String string)
  2. unquote(String string)
  3. unquote(String string)
  4. unquote(String string)
  5. unquote(String string)
  6. unquote(String string, char quote)
  7. unquote(String text)
  8. unquote(String text)
  9. unquote(String text)