Java String Double Quote doubleQuote(String value)

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

Description

Double quote the given string; double quotes are escaped.

License

Apache License

Parameter

Parameter Description
value The value to double quote.

Return

The double quoted string.

Declaration

public static String doubleQuote(String value) 

Method Source Code

//package com.java2s;
/*/*from  w  w w.ja  va  2 s  .  com*/
 *      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 {
    /**
     * Double quote the given string; double quotes are escaped.
     * If the given string is null, this method returns a quoted empty string ({@code ""}).
     *
     * @param value The value to double quote.
     * @return The double quoted string.
     */
    public static String doubleQuote(String value) {
        return quote(value, '"');
    }

    /**
     * Quote the given string; single quotes are escaped.
     * If the given string is null, this method returns a quoted empty string ({@code ''}).
     *
     * @param value The value to quote.
     * @return The quoted string.
     */
    public static String quote(String value) {
        return quote(value, '\'');
    }

    /**
     * Quotes text and escapes any existing quotes in the text.
     * {@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 surrounded in quotes with all existing quotes escaped with (i.e. ' becomes '')
     */
    private static String quote(String text, char quoteChar) {
        if (text == null || text.isEmpty())
            return emptyQuoted(quoteChar);

        int nbMatch = 0;
        int start = -1;
        do {
            start = text.indexOf(quoteChar, start + 1);
            if (start != -1)
                ++nbMatch;
        } while (start != -1);

        // no quotes found that need to be escaped, simply surround in quotes and return.
        if (nbMatch == 0)
            return quoteChar + text + quoteChar;

        // 2 for beginning and end quotes.
        // length for original text
        // nbMatch for escape characters to add to quotes to be escaped.
        int newLength = 2 + text.length() + nbMatch;
        char[] result = new char[newLength];
        result[0] = quoteChar;
        result[newLength - 1] = quoteChar;
        int newIdx = 1;
        for (int i = 0; i < text.length(); i++) {
            char c = text.charAt(i);
            if (c == quoteChar) {
                // escape quote with another occurrence.
                result[newIdx++] = c;
                result[newIdx++] = c;
            } else {
                result[newIdx++] = c;
            }
        }
        return new String(result);
    }

    /**
     * @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. doubleQuote(String s)
  2. doubleQuote(String str)
  3. doubleQuote(String str)
  4. doublequote(String str, boolean escapeHtml)
  5. doubleQuote(String string)
  6. doubleQuote(String x)
  7. doubleQuoted(String string)
  8. doubleQuotedString(String str)
  9. doublequoteEncode(String str)