Java String Quote quote(String string)

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

Description

quote

License

Apache License

Declaration

public static String quote(String string) 

Method Source Code

//package com.java2s;
/*//from w  w w  .jav  a  2  s .  c  o  m
 * #%L
 * SAMOA
 * %%
 * Copyright (C) 2013 Yahoo! 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.
 * #L%
 */

public class Main {
    public static String quote(String 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.indexOf('%') != -1) || (string.indexOf('\u001E') != -1)) {
            string = backQuoteChars(string);
            quote = true;
        }

        // Enclose the string in 's 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 = ("'".concat(string)).concat("'");
        }

        return string;
    }

    public static String backQuoteChars(String string) {

        int index;
        StringBuffer newStringBuffer;

        // replace each of the following characters with the backquoted version
        char charsFind[] = { '\\', '\'', '\t', '\n', '\r', '"', '%', '\u001E' };
        String charsReplace[] = { "\\\\", "\\'", "\\t", "\\n", "\\r", "\\\"", "\\%", "\\u001E" };
        for (int i = 0; i < charsFind.length; i++) {
            if (string.indexOf(charsFind[i]) != -1) {
                newStringBuffer = new StringBuffer();
                while ((index = string.indexOf(charsFind[i])) != -1) {
                    if (index > 0) {
                        newStringBuffer.append(string.substring(0, index));
                    }
                    newStringBuffer.append(charsReplace[i]);
                    if ((index + 1) < string.length()) {
                        string = string.substring(index + 1);
                    } else {
                        string = "";
                    }
                }
                newStringBuffer.append(string);
                string = newStringBuffer.toString();
            }
        }

        return string;
    }
}

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, boolean escapeHtml)
  7. quote(String string, String charsToQuote)
  8. quote(String string, StringBuilder w)
  9. quote(String tableName)