Java Java String Format toJava(CharSequence str)

Here you can find the source of toJava(CharSequence str)

Description

to Java

License

Apache License

Declaration

public static String toJava(CharSequence str) 

Method Source Code

//package com.java2s;
//License from project: Apache License 

public class Main {
    public static String toJava(CharSequence str) {
        if (str == null)
            return "null";
        int len = str.length();
        StringBuilder buf = new StringBuilder(len + 10);
        buf.append("\"");
        for (int i = 0; i < len; i++) {
            char c = str.charAt(i);
            switch (c) {
            case '\b':
                buf.append("\\b");
                break;
            case '\f':
                buf.append("\\f");
                break;
            case '\n':
                buf.append("\\n");
                break;
            case '\r':
                buf.append("\\r");
                break;
            case '\t':
                buf.append("\\t");
                break;
            //            case '\'':
            //                buf.append("\\\'");
            //                break;
            case '\"':
                buf.append("\\\"");
                break;
            case '\\':
                buf.append("\\\\");
                break;
            default:
                if (c >= ' ' && c < 128) {
                    // ASCII
                    buf.append(c);//from  w ww . ja  v  a  2  s .  co  m
                } else {
                    // Unicode
                    buf.append(String.format("\\u%04x", (int) c));
                }
            }
        }
        buf.append("\"");
        return buf.toString();
    }
}

Related

  1. convertToJavaClassname(final String classname, final Map replacement)
  2. convertToJavaIdentity(String original, boolean firstCharacterUpperCase, boolean normalize)
  3. convertToJavaUnicode(String input, Character start1, Character end1, Character start2, Character end2)
  4. toJava(final double[] doubles)
  5. toJava(String name)
  6. toJava(String text)
  7. toJAVA(String unicode)