Java String Unquote unquote(String string)

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

Description

unquotes are previously quoted string (but only if necessary), i.e., it removes the single quotes around it.

License

Open Source License

Parameter

Parameter Description
string the string to process

Return

the unquoted string

Declaration

public static String unquote(String string) 

Method Source Code

//package com.java2s;
/*//from   ww  w  .ja  v a 2s. c o m
 *   This program is free software: you can redistribute it and/or modify
 *   it under the terms of the GNU General Public License as published by
 *   the Free Software Foundation, either version 3 of the License, or
 *   (at your option) any later version.
 *
 *   This program is distributed in the hope that it will be useful,
 *   but WITHOUT ANY WARRANTY; without even the implied warranty of
 *   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 *   GNU General Public License for more details.
 *
 *   You should have received a copy of the GNU General Public License
 *   along with this program.  If not, see <http://www.gnu.org/licenses/>.
 */

public class Main {
    /**
     * unquotes are previously quoted string (but only if necessary), i.e., it
     * removes the single quotes around it. Inverse to quote(String).
     *
     * @param string   the string to process
     * @return      the unquoted string
     * @see      #quote(String)
     */
    public static String unquote(String string) {
        return unquote(string, "'");
    }

    /**
     * unquotes are previously quoted string (but only if necessary), i.e., it
     * removes the quote characters around it. Inverse to quote(String,String).
     *
     * @param string   the string to process
     * @param quoteChar   the quote character to use
     * @return      the unquoted string
     * @see      #quote(String,String)
     */
    public static String unquote(String string, String quoteChar) {
        if ((string == null) || (string.length() < 2))
            return string;

        if (string.startsWith(quoteChar) && string.endsWith(quoteChar)) {
            string = string.substring(1, string.length() - 1);

            if ((string.indexOf("\\n") != -1) || (string.indexOf("\\r") != -1) || (string.indexOf("\\'") != -1)
                    || (string.indexOf("\\\"") != -1) || (string.indexOf("\\\\") != -1)
                    || (string.indexOf("\\t") != -1)) {
                string = unbackQuoteChars(string);
            }
        }

        return string;
    }

    /**
     * The inverse operation of backQuoteChars().
     * Converts the specified strings into their character representations.
     *
     * @param string    the string
     * @param find   the string to find
     * @param replace   the character equivalents of the strings
     * @return       the converted string
     * @see      #backQuoteChars(String, char[], String[])
     */
    public static String unbackQuoteChars(String string, String[] find, char[] replace) {
        int index;
        StringBuilder newStr;
        int[] pos;
        int curPos;
        String str;
        int i;

        if (string == null)
            return null;

        pos = new int[find.length];

        str = new String(string);
        newStr = new StringBuilder();
        while (str.length() > 0) {
            // get positions and closest character to replace
            curPos = str.length();
            index = -1;
            for (i = 0; i < pos.length; i++) {
                pos[i] = str.indexOf(find[i]);
                if ((pos[i] > -1) && (pos[i] < curPos)) {
                    index = i;
                    curPos = pos[i];
                }
            }

            // replace character if found, otherwise finished
            if (index == -1) {
                newStr.append(str);
                str = "";
            } else {
                newStr.append(str.substring(0, pos[index]));
                newStr.append(replace[index]);
                str = str.substring(pos[index] + find[index].length());
            }
        }

        return newStr.toString();
    }

    /**
     * The inverse operation of backQuoteChars().
     * Converts back-quoted carriage returns and new lines in a string
     * to the corresponding character ('\r' and '\n').
     * Also "un"-back-quotes the following characters: ` " \ \t and %
     *
     * @param string    the string
     * @return       the converted string
     * @see      #backQuoteChars(String)
     */
    public static String unbackQuoteChars(String string) {
        return unbackQuoteChars(string, new String[] { "\\\\", "\\'", "\\t", "\\n", "\\r", "\\\"" },
                new char[] { '\\', '\'', '\t', '\n', '\r', '"' });
    }
}

Related

  1. unquote(String str)
  2. unquote(String string)
  3. unquote(String string)
  4. unquote(String string)
  5. unquote(String string)
  6. unquote(String string)
  7. unquote(String string)
  8. unquote(String string, char quote)
  9. unquote(String text)