Java String Quote quote(String inp)

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

Description

Escapes special characters in a string

License

Open Source License

Parameter

Parameter Description
inp the string to process

Return

The string with all of the special characters escaped.

Declaration

public static String quote(String inp) 

Method Source Code

//package com.java2s;
/*/*from  w  w w  .  j a  v  a2s.  c o m*/
 *EXHIBIT A - Sun Industry Standards Source License
 *
 *"The contents of this file are subject to the Sun Industry
 *Standards Source License Version 1.2 (the "License");
 *You may not use this file except in compliance with the
 *License. You may obtain a copy of the 
 *License at http://wbemservices.sourceforge.net/license.html
 *
 *Software distributed under the License is distributed on
 *an "AS IS" basis, WITHOUT WARRANTY OF ANY KIND, either
 *express or implied. See the License for the specific
 *language governing rights and limitations under the License.
 *
 *The Original Code is WBEM Services.
 *
 *The Initial Developer of the Original Code is:
 *Sun Microsystems, Inc.
 *
 *Portions created by: Sun Microsystems, Inc.
 *are Copyright 2002 Sun Microsystems, Inc.
 *
 *All Rights Reserved.
 *
 *Contributor(s): Brian Schlosser
 */

public class Main {
    /**
     * Escapes special characters in a string
     * 
     * @param inp the string to process
     * @return The string with all of the special characters escaped.
     */
    public static String quote(String inp) {
        StringBuffer sb = new StringBuffer(inp.length());
        sb.append('\"');
        sb.append(escape(inp));
        sb.append('\"');
        return sb.toString();
    }

    /**
     * Escapes special characters in a string
     * 
     * @param str the string to process
     * @return The string with all of the special characters escaped.
     */
    public static String escape(String str) {
        int size = str.length();
        StringBuffer sb = new StringBuffer(size);
        for (int i = 0; i < size; i++) {
            char ch = str.charAt(i);
            switch (ch) {
            case 0:
                continue;
            case '\n':
                sb.append("\\n");
                break;
            case '\t':
                sb.append("\\t");
                break;
            case '\b':
                sb.append("\\b");
                break;
            case '\r':
                sb.append("\\r");
                break;
            case '\f':
                sb.append("\\f");
                break;
            case '\\':
                sb.append("\\\\");
                break;
            case '\'':
                sb.append("\\\'");
                break;
            case '\"':
                sb.append("\\\"");
                break;
            default:
                if ((ch = str.charAt(i)) < 0x20 || ch > 0x7e) {
                    String s = Integer.toString(ch, 16);
                    sb.append("\\x" + "0000".substring(s.length() - 4) + s);
                } else {
                    sb.append(ch);
                }
                break;
            }
        }
        return sb.toString();
    }
}

Related

  1. quote(String content)
  2. quote(String content, String quoteMark)
  3. quote(String content, String quoteMark)
  4. quote(String entityName)
  5. quote(String filepath)
  6. quote(String input)
  7. quote(String input)
  8. quote(String literal)
  9. quote(String message, String quotationMark)