Java String Unquote unquote(String value)

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

Description

Unquote the given string if it is quoted; single quotes are unescaped.

License

Apache License

Parameter

Parameter Description
value The string to unquote.

Return

The unquoted string.

Declaration

public static String unquote(String value) 

Method Source Code

//package com.java2s;
/*//from  w w  w  . jav a2  s . co  m
 *      Copyright (C) 2012-2015 DataStax Inc.
 *
 *   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.
 */

public class Main {
    /**
     * Unquote the given string if it is quoted; single quotes are unescaped.
     * If the given string is not quoted, it is returned without any modification.
     *
     * @param value The string to unquote.
     * @return The unquoted string.
     */
    public static String unquote(String value) {
        return unquote(value, '\'');
    }

    /**
     * Unquotes text and unescapes non surrounding quotes.
     * {@code String.replace()} is a bit too inefficient (see JAVA-67, JAVA-1262).
     *
     * @param text      The text
     * @param quoteChar The character to use as a quote.
     * @return The text with surrounding quotes removed and non surrounding quotes unescaped (i.e. '' becomes ')
     */
    private static String unquote(String text, char quoteChar) {
        if (!isQuoted(text, quoteChar))
            return text;

        if (text.length() == 2)
            return "";

        String search = emptyQuoted(quoteChar);
        int nbMatch = 0;
        int start = -1;
        do {
            start = text.indexOf(search, start + 2);
            // ignore the second to last character occurrence, as the last character is a quote.
            if (start != -1 && start != text.length() - 2)
                ++nbMatch;
        } while (start != -1);

        // no escaped quotes found, simply remove surrounding quotes and return.
        if (nbMatch == 0)
            return text.substring(1, text.length() - 1);

        // length of the new string will be its current length - the number of occurrences.
        int newLength = text.length() - nbMatch - 2;
        char[] result = new char[newLength];
        int newIdx = 0;
        // track whenever a quoteChar is encountered and the previous character is not a quoteChar.
        boolean firstFound = false;
        for (int i = 1; i < text.length() - 1; i++) {
            char c = text.charAt(i);
            if (c == quoteChar) {
                if (firstFound) {
                    // The previous character was a quoteChar, don't add this to result, this action in
                    // effect removes consecutive quotes.
                    firstFound = false;
                } else {
                    // found a quoteChar and the previous character was not a quoteChar, include in result.
                    firstFound = true;
                    result[newIdx++] = c;
                }
            } else {
                // non quoteChar encountered, include in result.
                result[newIdx++] = c;
                firstFound = false;
            }
        }
        return new String(result);
    }

    /**
     * Return {@code true} if the given string is surrounded
     * by single quotes, and {@code false} otherwise.
     *
     * @param value The string to inspect.
     * @return {@code true} if the given string is surrounded
     * by single quotes, and {@code false} otherwise.
     */
    public static boolean isQuoted(String value) {
        return isQuoted(value, '\'');
    }

    /**
     * Return {@code true} if the given string is surrounded
     * by the quote character given, and {@code false} otherwise.
     *
     * @param value The string to inspect.
     * @return {@code true} if the given string is surrounded
     * by the quote character, and {@code false} otherwise.
     */
    private static boolean isQuoted(String value, char quoteChar) {
        return value != null && value.length() > 1 && value.charAt(0) == quoteChar
                && value.charAt(value.length() - 1) == quoteChar;
    }

    /**
     * @param quoteChar " or '
     * @return A quoted empty string.
     */
    private static String emptyQuoted(char quoteChar) {
        // don't handle non quote characters, this is done so that these are interned and don't create
        // repeated empty quoted strings.
        assert quoteChar == '"' || quoteChar == '\'';
        if (quoteChar == '"')
            return "\"\"";
        else
            return "''";
    }
}

Related

  1. unquote(String text)
  2. unquote(String text)
  3. unquote(String toUnquote, char quoteChar)
  4. unquote(String val)
  5. unquote(String val)
  6. unquote(String value)
  7. unquote(String value)
  8. UnQuote(String value)
  9. unquote(String value)