Java String Quote quoteJava(String s)

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

Description

Convert string to Java-compatible source form.

License

Open Source License

Declaration

public static String quoteJava(String s) 

Method Source Code

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

public class Main {
    /** //from  w ww .  j a v a  2 s .  c o m
     * Convert string to Java-compatible source form. 
     * */
    public static String quoteJava(String s) {
        StringBuilder b = new StringBuilder();
        b.append('"');
        quoteJavaContent(s, b);
        b.append('"');
        return b.toString();
    }

    /** 
     * Convert string to Java-compatible source form. 
     */
    public static void quoteJavaContent(String s, StringBuilder b) {
        for (int i = 0; i < s.length(); ++i) {
            char c = s.charAt(i);
            if (Character.isHighSurrogate(c)) {
                int codepoint = Character.codePointAt(s, i++);
                if (codepoint < 0xFFFF)
                    b.append("\\u" + hex(codepoint, "0000"));
                else
                    b.append("\\U" + hex(codepoint, "00000000"));
            } else
                quotedJavaChar(c, b);
        }
    }

    /** 
     * Return hex string for character inserted right adjusted in template. 
     */
    public static String hex(int c, String template) {
        String s = Integer.toHexString(c);
        return template.substring(s.length()) + s;
    }

    /**
     * Convert character to Java-compatible source form for use in a string.
     * @param c character
     * @param b stream to append to
     */
    public static void quotedJavaChar(char c, StringBuilder b) {
        switch (c) {
        case '\b':
            b.append("\\b");
            break;
        case '\t':
            b.append("\\t");
            break;
        case '\n':
            b.append("\\n");
            break;
        case '\f':
            b.append("\\f");
            break;
        case '\r':
            b.append("\\r");
            break;
        case '\'':
            b.append("\\'");
            break;
        case '\"':
            b.append("\\\"");
            break;
        case '\\':
            b.append("\\\\");
            break;
        default:
            if (Character.isISOControl(c))
                b.append("\\" + octal((int) c, "000"));
            else if (c <= '~')
                b.append(c);
            else
                b.append("\\u" + hex((int) c, "0000"));
        }
    }

    /**
     *  Return octal string for character inserted right adjusted in template. 
     */
    public static String octal(int c, String template) {
        String s = Integer.toOctalString(c);
        return template.substring(s.length()) + s;
    }
}

Related

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