Java String Quote quoteString(String str)

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

Description

Wrap the given string with single quote.

License

Apache License

Parameter

Parameter Description
str string to wrap

Return

wrapped string with single quote if the given string is not null

Declaration

public static String quoteString(String str) 

Method Source Code

//package com.java2s;
//License from project: Apache License 

public class Main {
    /**/*from ww  w  .  j  av a2 s  .c  om*/
     * Wrap the given string with single quote.
     * @param str string to wrap
     * @return wrapped string with single quote if the given string is not null
     */
    public static String quoteString(String str) {
        if (null == str) {
            return str;
        }
        StringBuffer dstStr = new StringBuffer(str.length() + 2);

        if (null != str) {
            dstStr.append("'").append(str).append("'");
        }

        return dstStr.toString();
    }

    /**
     * Wrap the string with single quote according to the <code>emptyWrap</code> flag.
     * <p>Examples:
     * <blockquote><pre>
     * StringUtil.quoteString("hello", true) return "'hello'"
     * StringUtil.quoteString("hello", false) return "'hello'"
     * StringUtil.quoteString("", true) return "''"
     * StringUtil.quoteString("", false) return ""
     * StringUtil.quoteString(null, true) return null
     * </pre></blockquote>
     * @param str string to wrap
     * @param emptyWrap flag tell whether wrap with single quote if the given
     * string is empty, true for wrap, false for not wrap
     * @return if the given string is null then return null, otherwise if given
     * string is not empty or the given <code>emptyWrap</code> is true, then
     * return wrapped string with single quote;if string is empty and 
     * <code>emptyWrap</code> is false, then return empty string.
     */
    public static String quoteString(String str, boolean emptyWrap) {
        if (str == null) {
            return null;
        }

        StringBuffer dstStr = new StringBuffer(str.length() + 2);

        if (str != null) {
            if (emptyWrap || (str.trim().length() > 0)) {
                dstStr.append("'").append(str).append("'");
            } else {
                return str;
            }
        }

        return dstStr.toString();
    }

    /**
     * Get string value of given Object <code>param</code>.
     * This method will invoke toString() method on given object if not null.
     * @param param object instance
     * @return string represent given object if it's not null, otherwise return null.
     */
    public static String toString(Object param) {
        if (param == null) {
            return null;
        }

        return param.toString().trim();
    }
}

Related

  1. quoteString(String s)
  2. quoteString(String s)
  3. QuoteString(String source)
  4. quoteString(String source, char quote)
  5. quoteString(String src)
  6. quoteString(String string)
  7. quoteString(String strVal)
  8. quoteString(String t)
  9. quoteString(String unquoted)