Java String quote( String str) Utility Method - Adding Single Quote to String

This page contains the source code of the Java String quote() utility method - This method quotes the given String with single quotes.

This utility method is useful if you want to add a single quote to the given String.

Java String quote( String str) Utility Method

The following String quote() utility method quotes the given String with single quotes:
/**
 * Quote the given {@code String} with single quotes.
 * @param str the input {@code String} (e.g. "myString")
 * @return the quoted {@code String} (e.g. "'myString'"),
 * or {@code null} if the input was {@code null}
 */

public static String quote( String str) {
 return (str != null ? "'" + str + "'" : null);
}

Related Utility Methods


Comments