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

Apache 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;
/*/* w  w w  .  j a  va  2s .com*/
 * #%L
 * SAMOA
 * %%
 * Copyright (C) 1999 - 2012 University of Waikato, Hamilton, New Zealand
 * %%
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 * 
 *      http://www.apache.org/licenses/LICENSE-2.0
 * 
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 * #L%
 */

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) {
        if (string.startsWith("'") && string.endsWith("'")) {
            string = string.substring(1, string.length() - 1);

            if ((string.contains("\\n")) || (string.contains("\\r")) || (string.contains("\\'"))
                    || (string.contains("\\\"")) || (string.contains("\\\\")) || (string.contains("\\t"))
                    || (string.contains("\\%")) || (string.contains("\\u001E"))) {
                string = unbackQuoteChars(string);
            }
        }

        return string;
    }

    /**
     * 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) {

        int index;
        StringBuffer newStringBuffer;

        // replace each of the following characters with the backquoted version
        String charsFind[] = { "\\\\", "\\'", "\\t", "\\n", "\\r", "\\\"", "\\%", "\\u001E" };
        char charsReplace[] = { '\\', '\'', '\t', '\n', '\r', '"', '%', '\u001E' };
        int pos[] = new int[charsFind.length];
        int curPos;

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

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

        return newStringBuffer.toString();
    }
}

Related

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