Java String Quote quoteIfString(Object obj)

Here you can find the source of quoteIfString(Object obj)

Description

Turn the given Object into a String with single quotes if it is a String; keeping the Object as-is else.

License

Open Source License

Parameter

Parameter Description
obj the input Object (e.g. "myString")

Return

the quoted String (e.g. "'myString'"), or the input object as-is if not a String

Declaration

public static Object quoteIfString(Object obj) 

Method Source Code

//package com.java2s;

public class Main {
    /**/*from  w w w  .ja  va 2s. co  m*/
     * Turn the given Object into a String with single quotes
     * if it is a String; keeping the Object as-is else.
     * @param obj the input Object (e.g. "myString")
     * @return the quoted String (e.g. "'myString'"),
     * or the input object as-is if not a String
     */
    public static Object quoteIfString(Object obj) {
        return (obj instanceof String ? quote((String) obj) : obj);
    }

    /**
     * Quote the given String with single quotes.
     * @param str the input String (e.g. "myString")
     * @return the quoted String (e.g. "'myString'"),
     * or <code>null<code> if the input was <code>null</code>
     */
    public static String quote(String str) {
        return (str != null ? "'" + str + "'" : null);
    }
}

Related

  1. quoteIfNeeded(StringBuilder buf, String str, String delim)
  2. quoteIfNotNull(String str)
  3. quoteIfNotNull(String text)
  4. quoteIfNotNull(StringBuilder sb, String val)
  5. quoteIfString(Object obj)
  6. quoteIfString(Object... arguments)
  7. quoteImpl(String s, char delim)
  8. quoteIt(String orig)
  9. quoteJava(String s)