Java String Quote quoteJavaString(String s)

Here you can find the source of quoteJavaString(String s)

Description

Convert a string to the Java literal and enclose it with double quotes.

License

Open Source License

Parameter

Parameter Description
s the text to convert

Return

the Java representation

Declaration

public static String quoteJavaString(String s) 

Method Source Code

//package com.java2s;
//License from project: Open Source License 

public class Main {
    /**/*w w w.ja  v a2  s .co  m*/
     * Convert a string to the Java literal and enclose it with double quotes.
     * Null will result in "null" (without double quotes).
     *
     * @param s the text to convert
     * @return the Java representation
     */
    public static String quoteJavaString(String s) {
        if (s == null) {
            return "null";
        }
        return "\"" + javaEncode(s) + "\"";
    }

    /**
     * Convert a string to a Java literal using the correct escape sequences.
     * The literal is not enclosed in double quotes. The result can be used in
     * properties files or in Java source code.
     *
     * @param s the text to convert
     * @return the Java representation
     */
    public static String javaEncode(String s) {
        int length = s.length();
        StringBuilder buff = new StringBuilder(length);
        for (int i = 0; i < length; i++) {
            char c = s.charAt(i);
            switch (c) {
            //            case '\b':
            //                // BS backspace
            //                // not supported in properties files
            //                buff.append("\\b");
            //                break;
            case '\t':
                // HT horizontal tab
                buff.append("\\t");
                break;
            case '\n':
                // LF linefeed
                buff.append("\\n");
                break;
            case '\f':
                // FF form feed
                buff.append("\\f");
                break;
            case '\r':
                // CR carriage return
                buff.append("\\r");
                break;
            case '"':
                // double quote
                buff.append("\\\"");
                break;
            case '\\':
                // backslash
                buff.append("\\\\");
                break;
            default:
                int ch = c & 0xffff;
                if (ch >= ' ' && (ch < 0x80)) {
                    buff.append(c);
                    // not supported in properties files
                    // } else if(ch < 0xff) {
                    // buff.append("\\");
                    // // make sure it's three characters (0x200 is octal 1000)
                    // buff.append(Integer.toOctalString(0x200 | ch).substring(1));
                } else {
                    buff.append("\\u");
                    String hex = Integer.toHexString(ch);
                    // make sure it's four characters
                    for (int len = hex.length(); len < 4; len++) {
                        buff.append('0');
                    }
                    buff.append(hex);
                }
            }
        }
        return buff.toString();
    }
}

Related

  1. quoteIfString(Object... arguments)
  2. quoteImpl(String s, char delim)
  3. quoteIt(String orig)
  4. quoteJava(String s)
  5. quoteJavascriptString(String s)
  6. quoteJavaString(String s)
  7. quoteJsonLib(String string)
  8. quoteLiteralAsRegexp(String text)
  9. quoteLocation(String location)