Java String Quote quote(String val, char quote)

Here you can find the source of quote(String val, char quote)

Description

Quote a string, escaping it into C/C++ style

License

Open Source License

Parameter

Parameter Description
val incoming "pure" string
quote the quote character

Return

an escaped string surrounded with quotes.

Declaration

static public String quote(String val, char quote) 

Method Source Code

//package com.java2s;
/*// w ww. j a v  a  2s .c  o  m
* Copyright (c) 2009 Nokia Corporation and/or its subsidiary(-ies).
* All rights reserved.
* This component and the accompanying materials are made available
* under the terms of the License "Eclipse Public License v1.0"
* which accompanies this distribution, and is available
* at the URL "http://www.eclipse.org/legal/epl-v10.html".
*
* Initial Contributors:
* Nokia Corporation - initial contribution.
*
* Contributors:
*
* Description: 
*
*/

public class Main {
    /**
      * Quote a string, escaping it into C/C++ style
      * @param val incoming "pure" string
      * @param quote the quote character
      * @return an escaped string surrounded with quotes. 
      */
    static public String quote(String val, char quote) {
        StringBuffer buff = new StringBuffer();
        buff.append(quote);
        buff.append(escape(val, quote));
        buff.append(quote);
        return buff.toString();
    }

    /**
     * Escape a string 'val' into C/C++ style, doubling
     * backslashes and escaping the quote character.
     * @param val incoming "pure" string
     * @param quote the quote character
     * @return the string with interesting characters escaped
     */
    static public String escape(String val, char quote) {
        StringBuffer buff = new StringBuffer();
        for (int i = 0; i < val.length(); i++) {
            char ch = val.charAt(i);
            if (ch == quote || ch == '\\') {
                buff.append('\\');
                buff.append(ch);
            } else if (ch < 32) {
                switch (ch) {
                case '\t':
                    buff.append("\\t"); //$NON-NLS-1$
                    break;
                case '\n':
                    buff.append("\\n"); //$NON-NLS-1$
                    break;
                case '\r':
                    buff.append("\\r"); //$NON-NLS-1$
                    break;
                case '\f':
                    buff.append("\\f"); //$NON-NLS-1$
                    break;
                case '\b':
                    buff.append("\\b"); //$NON-NLS-1$
                    break;
                default:
                    buff.append("\\");
                    buff.append(String.format("%03o", Integer.valueOf(ch)));
                    break;
                }
            } else if (isLineOrParaSeparator(ch)) {
                buff.append("\\u"); //$NON-NLS-1$
                buff.append(Integer.toHexString(ch));
            } else
                buff.append(ch);
        }
        return buff.toString();
    }

    private static boolean isLineOrParaSeparator(char ch) {
        int type = Character.getType(ch);
        return ch > 127 && (type == Character.PARAGRAPH_SEPARATOR || type == Character.LINE_SEPARATOR);
    }
}

Related

  1. quote(String text, String quote)
  2. quote(String toQuote, String specials, char quoteChar)
  3. quote(String txt)
  4. quote(String val)
  5. quote(String val)
  6. quote(String value)
  7. quote(String value)
  8. quote(String value)
  9. quote(String value)