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

StringquotedStringOrNull(String string)
Returns a string surrounded with quotes, or an unquoted string "null"
return (string == null) ? "null" : ("\"" + string.replace("\\", "\\\\").replace("\"", "\\\"") + "\"");
StringquoteEach(String string)
quote Each
String[] pieces = string.split("(\\s)+");
StringBuffer result = new StringBuffer();
for (String piece : pieces) {
    if (piece.indexOf("\"") >= 0) {
        return string;
    if (!piece.isEmpty()) {
        result.append(" \"");
...
StringquoteEscape(final String original)
Quote and escape input value for CSV
String result = original;
if (result.indexOf('\"') >= 0) {
    result = result.replace("\"", ESCAPED_QUOTE);
if (result.indexOf(COMMA) >= 0) {
    result = "\"" + result + "\"";
return result;
...
StringquoteEscaped(String input)
quote Escaped
return input.replace("\\", "\\\\").replace("\"", "\\\"").replaceAll("[\\p{Z}\\s]", " ").trim();
StringquoteExecutable(String executable)
Surrounds the executable with double quotes if a blank is in the path.
String result;
result = executable;
if (result.indexOf(' ') > -1)
    result = "\"" + result + "\"";
return result;
StringquoteFileName(String filename)
Quotes a file name if it contains whitespaces and has not already been quoted.
if (filename.startsWith("\"") || filename.endsWith("\"")) {
    return filename;
if (System.getProperty("os.name").toLowerCase().startsWith("windows") && filename.startsWith("file://")) {
    filename = filename.substring(7);
if (filename.matches(".*\\s+?.*")) {
    return '"' + filename + '"';
...
StringquoteForBatchScript(String arg)
Quote a command argument for a command to be run by a Windows batch script, if the argument needs quoting.
boolean needsQuotes = false;
for (int i = 0; i < arg.length(); i++) {
    int c = arg.codePointAt(i);
    if (Character.isWhitespace(c) || c == '"' || c == '=' || c == ',' || c == ';') {
        needsQuotes = true;
        break;
if (!needsQuotes) {
    return arg;
StringBuilder quoted = new StringBuilder();
quoted.append("\"");
for (int i = 0; i < arg.length(); i++) {
    int cp = arg.codePointAt(i);
    switch (cp) {
    case '"':
        quoted.append('"');
        break;
    default:
        break;
    quoted.appendCodePoint(cp);
if (arg.codePointAt(arg.length() - 1) == '\\') {
    quoted.append("\\");
quoted.append("\"");
return quoted.toString();
StringquoteForCommandString(String s)
Quotes a string so that it can be used in a command string.
StringBuilder quoted = new StringBuilder().append('"');
for (int i = 0; i < s.length(); i++) {
    int cp = s.codePointAt(i);
    if (cp == '"' || cp == '\\') {
        quoted.appendCodePoint('\\');
    quoted.appendCodePoint(cp);
return quoted.append('"').toString();
StringquoteForCsv(final String field)
Inserts quotation marks around a field if it contains commas
if (field != null && field.contains(",")) {
    return "\"" + field + "\"";
return field;
StringquoteForHTML(String toQuote)
Quotes each and every character, e.g.
StringBuilder result = new StringBuilder();
for (int i = 0; i < toQuote.length(); ++i) {
    result.append("&#").append((int) toQuote.charAt(i)).append(';');
return result.toString();