Java String Quote quoteD(String s)

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

Description

Take a string and delimit it with double quotes.

License

Open Source License

Parameter

Parameter Description
s The input string.

Return

The double quotes delimited string; if the input is null returns null.

Declaration

public static String quoteD(String s) 

Method Source Code

//package com.java2s;
/*******************************************************************************
 * Copyright (c) 2012 Firestar Software, Inc.
 * 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://from  w  w w  .  j av  a  2 s  . c om
 *     Firestar Software, Inc. - initial API and implementation
 *
 * Author:
 *     Gabriel Oancea
 *
 *******************************************************************************/

public class Main {
    /**
     * Take a string and delimit it with double quotes. If there are any double quotes in the string escape them. For
     * example:
     * <p>
     * <code>Arthur has a 36" sword</code>
     * <p>
     * becomes:
     * <p>
     * <code>"Arthur has a 36"" sword"</code>
     * 
     * @param s The input string.
     * @return The double quotes delimited string; if the input is null returns null.
     */
    public static String quoteD(String s) {
        return quoteImpl(s, '\"');
    }

    private static String quoteImpl(String s, char delim) {
        if (s == null)
            return null;
        StringBuffer sb = new StringBuffer(s.length() + 3); // two delims one
        // magic
        sb.append(delim);
        for (int i = 0; i < s.length(); i++) {
            char c = s.charAt(i);
            if (c == delim)
                sb.append(delim);
            sb.append(c);
        }
        sb.append(delim);
        return sb.toString();
    }
}

Related

  1. quoted(CharSequence s)
  2. quoted(final String str)
  3. quoted(String i, char q)
  4. quoted(String path)
  5. quoted(String s)
  6. quoted(String str)
  7. quoted(String text)
  8. quoted(String val, boolean wrap)
  9. quoted(String value)