Java String Quote quote(CharSequence str)

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

Description

quote

License

Apache License

Declaration

public static String quote(CharSequence str) 

Method Source Code

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

public class Main {
    public static String quote(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 '\n':
            case '\r':
                buf.append(c);//from  ww  w. j  av a  2  s.  c o m
            case '\t':
                buf.append("\\t");
                break;
            case '\"':
                buf.append("\\\"");
                break;
            case '\\':
                buf.append("\\\\");
                break;
            default:
                if (c >= ' ' && c < 65000) {
                    // ASCII
                    buf.append(c);
                } else {
                    // Unicode
                    buf.append(String.format("\\u%04x", (int) c));
                }
            }
        }
        buf.append("\"");
        return buf.toString();
    }
}

Related

  1. quote(CharSequence cs)
  2. quote(CharSequence value)
  3. quote(final CharSequence original)
  4. quote(final CharSequence phrase)
  5. quote(final String s)