Java Utililty Methods String Quote

List of utility methods to do String Quote

Description

The list of methods to do String Quote are organized into topic(s).

Method

StringquoteIdentifier(String identifier, boolean isPedantic)
Surrounds identifier with "`" and duplicates these symbols inside the identifier.
return quoteIdentifier(identifier, "`", isPedantic);
StringquoteIdentifier(String identifier, String quoteChar)
Quotes an identifier, escaping any dangling quotes within
if (identifier == null) {
    return null;
identifier = identifier.trim();
int quoteCharLength = quoteChar.length();
if (quoteCharLength == 0 || " ".equals(quoteChar)) {
    return identifier;
if (identifier.startsWith(quoteChar) && identifier.endsWith(quoteChar)) {
    String identifierQuoteTrimmed = identifier.substring(quoteCharLength,
            identifier.length() - quoteCharLength);
    int quoteCharPos = identifierQuoteTrimmed.indexOf(quoteChar);
    while (quoteCharPos >= 0) {
        int quoteCharNextExpectedPos = quoteCharPos + quoteCharLength;
        int quoteCharNextPosition = identifierQuoteTrimmed.indexOf(quoteChar, quoteCharNextExpectedPos);
        if (quoteCharNextPosition == quoteCharNextExpectedPos) {
            quoteCharPos = identifierQuoteTrimmed.indexOf(quoteChar,
                    quoteCharNextPosition + quoteCharLength);
        } else {
            break;
    if (quoteCharPos < 0) {
        return identifier;
return quoteChar + identifier.replaceAll(quoteChar, quoteChar + quoteChar) + quoteChar;
StringquoteIdentifier(String s)
Enclose a string with double quotes.
int length = s.length();
StringBuilder buff = new StringBuilder(length + 2);
buff.append('\"');
for (int i = 0; i < length; i++) {
    char c = s.charAt(i);
    if (c == '"') {
        buff.append(c);
    buff.append(c);
return buff.append('\"').toString();
StringquoteIdentifier(String s)
Contributed by Thomas Mueller to handle doubling quote characters found in an identifier.
if (s == null) {
    return null;
StringBuilder buff = null;
buff = new StringBuilder();
for (int i = 0; i < s.length(); i++) {
    char c = s.charAt(i);
    if (c == '"' && i != 0 && i != s.length() - 1) {
...
StringquoteIfCeylonKeyword(String name)
Prefixes the given name with a "\i" if it is a Ceylon keyword
if (isCeylonKeyword(name))
    return "\\i" + name;
return name;
StringquoteIfNeeded(String id)
quote If Needed
if (id == null) {
    return null;
} else if (quoteNeeded(id)) {
    return SINGLE_QUOTE_STRING + id + SINGLE_QUOTE_STRING;
return id;
StringquoteIfNeeded(String input)
quote If Needed
if (isBlank(input))
    return input;
String quote = "\'";
boolean needQuote = false;
if (input.indexOf('\'') > -1) {
    quote = "\"";
    needQuote = true;
if (input.indexOf(' ') > -1) {
    needQuote = true;
if (needQuote) {
    return quote + input + quote;
return input;
StringquoteIfNeeded(String source)
Add quotes around the object iff it's not a JSON object.
String trimmed = source.trim();
if (isObject(trimmed) || isArray(trimmed) || isString(trimmed) || isBoolean(trimmed) || isNull(trimmed)
        || isNumber(trimmed)) {
    return source;
} else {
    return "\"" + source + "\"";
StringquoteIfNeeded(String value)
quote If Needed
if (value == null) {
    return null;
return value.contains(SPACE) ? String.format("'%s'", value) : value;
voidquoteIfNeeded(StringBuilder buf, String str, String delim)
Append into buf the provided string, adding quotes if needed.
if (str == null) {
    return;
int len = str.length();
if (len == 0) {
    return;
int ch;
...