Java String Quote quote(String string)

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

Description

Quotes a string if it contains special characters.

License

Open Source License

Parameter

Parameter Description
string the string to be quoted

Return

the string (possibly quoted)

Declaration

public static String quote(String string) 

Method Source Code

//package com.java2s;
/*//from  w  ww.j a  v  a2s.co 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 {
    /**
     * Quotes a string if it contains special characters.
     *
     * The following rules are applied:
     *
     * A character is backquoted version of it is one
     * of <tt>" ' % \ \n \r \t</tt>.
     *
     * A string is enclosed within single quotes if a character has been
     * backquoted using the previous rule above or contains
     * <tt>{ }</tt> or is exactly equal to the strings
     * <tt>, ? space or ""</tt> (empty string).
     *
     * A quoted question mark distinguishes it from the missing value which
     * is represented as an unquoted question mark in arff files.
     *
     * @param string    the string to be quoted
     * @return       the string (possibly quoted)
     * @see      #unquote(String)
     */
    public static String quote(String string) {
        return quote(string, "'");
    }

    /**
     * Quotes a string if it contains special characters.
     *
     * The following rules are applied:
     *
     * A character is backquoted version of it is one
     * of <tt>" ' % \ \n \r \t</tt>.
     *
     * A string is enclosed within the quote character if a character has been
     * backquoted using the previous rule above or contains
     * <tt>{ }</tt> or is exactly equal to the strings
     * <tt>, ? space or ""</tt> (empty string).
     *
     * A quoted question mark distinguishes it from the missing value which
     * is represented as an unquoted question mark in arff files.
     *
     * @param string    the string to be quoted
     * @param quoteChar   the quote character to use
     * @return       the string (possibly quoted)
     * @see      #unquote(String,String)
     */
    public static String quote(String string, String quoteChar) {
        if (string == null)
            return string;

        boolean quote = false;

        // backquote the following characters
        if ((string.indexOf('\n') != -1) || (string.indexOf('\r') != -1) || (string.indexOf('\'') != -1)
                || (string.indexOf('"') != -1) || (string.indexOf('\\') != -1) || (string.indexOf('\t') != -1)) {
            string = backQuoteChars(string);
            quote = true;
        }

        // Enclose the string in quotes if the string contains a recently added
        // backquote or contains one of the following characters.
        if ((quote == true) || (string.indexOf('{') != -1) || (string.indexOf('}') != -1)
                || (string.indexOf(',') != -1) || (string.equals("?")) || (string.indexOf(' ') != -1)
                || (string.equals(""))) {
            string = (new String(quoteChar).concat(string)).concat(new String(quoteChar));
        }

        return string;
    }

    /**
     * Converts specified characters into the string equivalents.
     *
     * @param string    the string
     * @param find   the characters to replace
     * @param replace   the replacement strings for the characters
     * @return       the converted string
     * @see      #unbackQuoteChars(String, String[], char[])
     */
    public static String backQuoteChars(String string, char[] find, String[] replace) {
        int index;
        StringBuilder newStr;
        int i;

        if (string == null)
            return string;

        for (i = 0; i < find.length; i++) {
            if (string.indexOf(find[i]) != -1) {
                newStr = new StringBuilder();
                while ((index = string.indexOf(find[i])) != -1) {
                    if (index > 0)
                        newStr.append(string.substring(0, index));
                    newStr.append(replace[i]);
                    if ((index + 1) < string.length())
                        string = string.substring(index + 1);
                    else
                        string = "";
                }
                newStr.append(string);
                string = newStr.toString();
            }
        }

        return string;
    }

    /**
     * Converts carriage returns and new lines in a string into \r and \n.
     * Backquotes the following characters: ` " \ \t and %
     *
     * @param string    the string
     * @return       the converted string
     * @see      #unbackQuoteChars(String)
     */
    public static String backQuoteChars(String string) {
        return backQuoteChars(string, new char[] { '\\', '\'', '\t', '\n', '\r', '"' },
                new String[] { "\\\\", "\\'", "\\t", "\\n", "\\r", "\\\"" });
    }
}

Related

  1. quote(String string)
  2. quote(String string)
  3. quote(String string)
  4. quote(String string)
  5. quote(String string)
  6. quote(String string)
  7. quote(String string)
  8. quote(String string)
  9. quote(String string, boolean escapeHtml)