Java String Quote quote(String str)

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

Description

Quotifies a specified string.

License

Open Source License

Parameter

Parameter Description
str the string to quotify

Return

quotified and escaped string

Declaration

public static String quote(String str) 

Method Source Code

//package com.java2s;
/*/* w  w w. ja  va2  s  . c  o  m*/
 * Portions of this file Copyright 1999-2005 University of Chicago
 * Portions of this file Copyright 1999-2005 The University of Southern California.
 *
 * This file or a portion of this file is licensed under the
 * terms of the Globus Toolkit Public License, found at
 * http://www.globus.org/toolkit/download/license.html.
 * If you redistribute this file, with or without
 * modifications, you must include this notice in the file.
 */

public class Main {
    /**
     * Quotifies a specified string.
     * The entire string is encompassed by double quotes and each
     * " is replaced with \", \ is replaced with \\.
     *
     * @param str the string to quotify
     * @return quotified and escaped string
     */
    public static String quote(String str) {
        int len = str.length();
        StringBuffer buf = new StringBuffer(len + 2);
        buf.append("\"");
        char c;
        for (int i = 0; i < len; i++) {
            c = str.charAt(i);
            if (c == '"' || c == '\\') {
                buf.append("\\");
            }
            buf.append(c);
        }
        buf.append("\"");
        return buf.toString();
    }
}

Related

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