Encasing a string in double quotes I need to add some characters to a String before using it for an API call. The idea is to add a " char both at the beginning and at the end of the String, to make sure the API does an exact search for the input string.

Version 1:

StringBuilder(escapeJava(query))
                .insert(0, DOUBLE_QUOTE_CHAR)
                .append(DOUBLE_QUOTE_CHAR)
                .toString();


Version 2:

String.format("%c" + escapeJava(query) + "%c", DOUBLE_QUOTE_CHAR, DOUBLE_QUOTE_CHAR)


I prefer the readability of the first one, but I don't think it will be as quick as the second version, and might look like overkill.
Version 1 uses insert(0, ...) which has to move over all the characters already in the StringBuilder's buffer, so is unnecessarily wasting time.

A more efficient construct would be:

StringBuilder().append(DOUBLE_QUOTE_CHAR)
               .append(escapeJava(query))
               .append(DOUBLE_QUOTE_CHAR)
               .toString();


Version 2 uses StringBuilder under the hood, to construct "%c" + escapeJava(query) + "%c", so your belief the second version is faster may be in error.

Which leads us to the most efficient, most readable version:

DOUBLE_QUOTE_CHAR + escapeJava(query) + DOUBLE_QUOTE_CHAR;