Java String Quote quote(String s)

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

Description

Quote the given string by replacing unprintable characters by escape sequences.

License

Apache License

Parameter

Parameter Description
s the string to be quoted.

Return

a string which is a quoted representation of the input string.

Declaration

public static String quote(String s) 

Method Source Code

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

public class Main {
    /** Quote the given string by replacing unprintable characters by escape
    sequences./*from  w  w  w.jav a  2  s  .c om*/
         
    From PXLab at 
    http://www.uni-mannheim.de/fakul/psycho/irtel/pxlab/index-download.html
           
    @author Hans Irtel, with mods by Dave Voorhis
    @version 0.1.9
        
    @param s the string to be quoted.
    @return a string which is a quoted representation of the input
    string. */
    public static String quote(String s) {
        char[] in = s.toCharArray();
        int n = in.length;
        StringBuffer out = new StringBuffer(n);
        for (int i = 0; i < n; i++) {
            switch (in[i]) {
            case '\n':
                out.append("\\n");
                break;
            case '\t':
                out.append("\\t");
                break;
            case '\b':
                out.append("\\b");
                break;
            case '\r':
                out.append("\\r");
                break;
            case '\f':
                out.append("\\f");
                break;
            case '\\':
                out.append("\\\\");
                break;
            case '\'':
                out.append("\\\'");
                break;
            case '\"':
                out.append("\\\"");
                break;
            default:
                out.append(new String(in, i, 1));
                break;
            }
        }
        return (out.toString());
    }
}

Related

  1. quote(String p_sql)
  2. quote(String p_string)
  3. quote(String path)
  4. quote(String s)
  5. quote(String s)
  6. quote(String s)
  7. quote(String s)
  8. quote(String s)
  9. quote(String s)