Java String Quote quote(String string)

Here you can find the source of quote(String string)

Description

Like #escape but puts a double quote character ('\"') around the escaped string.

License

Open Source License

Declaration

public static String quote(String string) 

Method Source Code

//package com.java2s;

public class Main {
    /**// w  ww .  j  av  a 2s . co  m
     * Like {@link #escape} but puts a double quote character (<tt>'\"'</tt>)
     * around the escaped string.
     */
    public static String quote(String string) {
        StringBuilder buffer = new StringBuilder();

        buffer.append('\"');
        escape(string, buffer);
        buffer.append('\"');

        return buffer.toString();
    }

    /**
     * Returns the specified string with all non-ASCII characters and
     * non-printable ASCII characters replaced by the corresponding Java
     * escape sequences (that is <tt>'\n'</tt>, <tt>'\u00E9'</tt>, etc).
     * 
     * @param string the String to be escaped
     * @return the specified string with all non-ASCII characters and
     * non-printable ASCII characters replaced by the corresponding Java
     * escape sequences
     */
    public static String escape(String string) {
        StringBuilder buffer = new StringBuilder();
        escape(string, buffer);
        return buffer.toString();
    }

    /**
     * Same as {@link #escape(String)} except that the escaped string is
     * appended to specified buffer.
     */
    public static void escape(String string, StringBuilder buffer) {
        int length = string.length();
        for (int i = 0; i < length; ++i) {
            char c = string.charAt(i);

            switch (c) {
            case '\b':
                buffer.append("\\b");
                break;
            case '\t':
                buffer.append("\\t");
                break;
            case '\n':
                buffer.append("\\n");
                break;
            case '\f':
                buffer.append("\\f");
                break;
            case '\r':
                buffer.append("\\r");
                break;
            case '\"':
                buffer.append("\\\"");
                break;
            case '\'':
                buffer.append("\\'");
                break;
            case '\\':
                buffer.append("\\\\");
                break;
            default:
                if (c >= 0x0020 && c <= 0x007E) {
                    buffer.append(c);
                } else {
                    escape(c, buffer);
                }
            }
        }
    }

    /**
     * Returns the <tt>\\u<i>XXXX</i></tt> Java escape sequence corresponding
     * to specified character.
     * 
     * @param c the character to be escaped
     * @return A <tt>\\u<i>XXXX</i></tt> Java escape sequence
     */
    public static String escape(char c) {
        StringBuilder buffer = new StringBuilder();
        escape(c, buffer);
        return buffer.toString();
    }

    /**
     * Same as {@link #escape(char)} expect that the <tt>\\u<i>XXXX</i></tt>
     * Java escape sequence is appended to specified buffer.
     */
    public static void escape(char c, StringBuilder buffer) {
        buffer.append("\\u");

        String hex = Integer.toString((int) c, 16);

        int hexLength = hex.length();
        while (hexLength < 4) {
            buffer.append('0');
            ++hexLength;
        }

        buffer.append(hex);
    }
}

Related

  1. quote(String string)
  2. quote(String string)
  3. quote(String string)
  4. quote(String string)
  5. quote(String string)
  6. quote(String string)
  7. quote(String string)
  8. quote(String string)
  9. quote(String string)