Java String Quote quoteString(String t)

Here you can find the source of quoteString(String t)

Description

quote String

License

Open Source License

Declaration

public static String quoteString(String t) 

Method Source Code

//package com.java2s;
/**//ww  w.j a  v a 2  s. co  m
 * Copyright 2004-2014 Riccardo Solmi. All rights reserved.
 * This file is part of the Whole Platform.
 *
 * The Whole Platform is free software: you can redistribute it and/or modify
 * it under the terms of the GNU Lesser General Public License as published by
 * the Free Software Foundation, either version 3 of the License, or
 * (at your option) any later version.
 *
 * The Whole Platform 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 Lesser General Public License for more details.
 *
 * You should have received a copy of the GNU Lesser General Public License
 * along with the Whole Platform. If not, see <http://www.gnu.org/licenses/>.
 */

public class Main {
    public static String quoteString(String t) {
        return "\"" + escapeString(t, true, false) + "\"";
    }

    public static String escapeString(String t, boolean escapeDoubleQuotes, boolean escapeSingleQuotes) {
        StringBuilder r = new StringBuilder();
        for (int j = 0; j < t.length(); ++j) {
            char c = t.charAt(j);
            if (c == ' ')
                r.append(' ');
            else if (c == '\n')
                r.append("\\n");
            else if (c == '\r')
                r.append("\\r");
            else if (c == '"' && escapeDoubleQuotes)
                r.append("\\\"");
            else if (c == '\'' && escapeSingleQuotes)
                r.append("\\\'");
            else if (c == '\t')
                r.append("\\t");
            else if (c == '\b')
                r.append("\\b");
            else if (c == '\f')
                r.append("\\f");
            else if (c == '\\')
                r.append("\\\\");
            else if (Character.isISOControl(c) || Character.isWhitespace(c)) {
                r.append("\\u");
                r.append(charToHex(c));
            } else
                r.append(c);
        }
        return r.toString();
    }

    public static boolean isWhitespace(CharSequence cs) {
        int count = 0, length = cs.length();
        while (count < length && Character.isWhitespace(cs.charAt(count)))
            count++;
        return count == length;
    }

    public static String charToHex(char c) {
        // Returns hex String representation of char c
        byte hi = (byte) (c >>> 8);
        byte lo = (byte) (c & 0xff);
        return byteToHex(hi) + byteToHex(lo);
    }

    public static String byteToHex(byte b) {
        // Returns hex String representation of byte b
        char hexDigit[] = { '0', '1', '2', '3', '4', '5', '6', '7', '8', '9', 'a', 'b', 'c', 'd', 'e', 'f' };
        char[] array = { hexDigit[(b >> 4) & 0x0f], hexDigit[b & 0x0f] };
        return new String(array);
    }
}

Related

  1. quoteString(String source, char quote)
  2. quoteString(String src)
  3. quoteString(String str)
  4. quoteString(String string)
  5. quoteString(String strVal)
  6. quoteString(String unquoted)
  7. quoteString(String value, boolean force)
  8. quoteStringLiteral(String string)
  9. quoteStringSQL(String s)