Java String Single Quote singleQuote(final String value)

Here you can find the source of singleQuote(final String value)

Description

Enclose the input string in single quotes if not already.

License

Open Source License

Declaration

public static String singleQuote(final String value) 

Method Source Code

//package com.java2s;
/*//from w w w .j  a va2  s .  c  om
 * #%L
 * Tesora Inc.
 * Database Virtualization Engine
 * %%
 * Copyright (C) 2011 - 2014 Tesora Inc.
 * %%
 * This program is free software: you can redistribute it and/or modify
 * it under the terms of the GNU Affero General Public License, version 3,
 * as published by the Free Software Foundation.
 * 
 * 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 Affero General Public License for more details.
 * 
 * You should have received a copy of the GNU Affero General Public License
 * along with this program. If not, see <http://www.gnu.org/licenses/>.
 * #L%
 */

public class Main {
    private static final String SINGLE_QUOTE = "'";
    private static final String DOUBLE_QUOTE = "\"";
    private static final String BACK_QUOTE = "`";

    /**
     * Enclose the input string in single quotes if not already.
     */
    public static String singleQuote(final String value) {
        if ((value != null) && !value.isEmpty()
                && !isQuoted(value, SINGLE_QUOTE)) {
            return SINGLE_QUOTE.concat(value).concat(SINGLE_QUOTE);
        }

        return value;
    }

    public static boolean isQuoted(final String value) {
        return (isQuoted(value, SINGLE_QUOTE)
                || isQuoted(value, DOUBLE_QUOTE) || isQuoted(value,
                    BACK_QUOTE));
    }

    private static boolean isQuoted(final String value, final String quote) {
        return value.startsWith(quote) && value.endsWith(quote);
    }
}

Related

  1. singleQuote(final String text)
  2. singleQuote(Object thing)
  3. singleQuote(String data)
  4. singleQuote(String input)
  5. singleQuote(String s)