Java String Quote quote(String s)

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

Description

Returns a literal replacement String for the specified String.

License

Open Source License

Parameter

Parameter Description
s a parameter

Declaration

private static String quote(String s) 

Method Source Code

//package com.java2s;
/*******************************************************************************
 * Copyright (c) 2004, 2008 Actuate Corporation.
 * All rights reserved. This program and the accompanying materials
 * are made available under the terms of the Eclipse Public License v1.0
 * which accompanies this distribution, and is available at
 * http://www.eclipse.org/legal/epl-v10.html
 *
 * Contributors://  w  ww.  j a v a 2 s .  c o  m
 *  Actuate Corporation  - initial API and implementation
 *******************************************************************************/

public class Main {
    /**
     * Returns a literal replacement <code>String</code> for the specified
     * <code>String</code>.
     * @param s
     * @return
     */
    private static String quote(String s) {
        if ((s.indexOf('\\') == -1) && (s.indexOf('$') == -1))
            return s;
        StringBuffer sb = new StringBuffer();
        for (int i = 0; i < s.length(); i++) {
            char c = s.charAt(i);
            if (c == '\\') {
                sb.append('\\');
                sb.append('\\');
            } else if (c == '$') {
                sb.append('\\');
                sb.append('$');
            } else {
                sb.append(c);
            }
        }
        return sb.toString();
    }
}

Related

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