Java String Quote quotedJavaChar(char c, StringBuilder b)

Here you can find the source of quotedJavaChar(char c, StringBuilder b)

Description

Convert character to Java-compatible source form for use in a string.

License

Open Source License

Parameter

Parameter Description
c character
b stream to append to

Declaration

public static void quotedJavaChar(char c, StringBuilder b) 

Method Source Code

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

public class Main {
    /**//from ww  w  .  j  ava2  s.  c om
     * 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;
    }

    /** 
     * 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;
    }
}

Related

  1. quoted(String val, boolean wrap)
  2. quoted(String value)
  3. quoted(String value, boolean addQuotes)
  4. quotedEscape(final String string)
  5. quotedIndexOf(String text, int start, int limit, String setOfChars)
  6. quotedName(String name)
  7. quotedName(String name)
  8. quotedName(String name)
  9. quotedOrNULL(String str)