Here you can find the source of quoteString(String str)
Parameter | Description |
---|---|
str | string to wrap |
public static String quoteString(String str)
//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(); } }