List of utility methods to do String Quote
String | quotedStringOrNull(String string) Returns a string surrounded with quotes, or an unquoted string "null" return (string == null) ? "null" : ("\"" + string.replace("\\", "\\\\").replace("\"", "\\\"") + "\""); |
String | quoteEach(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(" \""); ... |
String | quoteEscape(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; ... |
String | quoteEscaped(String input) quote Escaped return input.replace("\\", "\\\\").replace("\"", "\\\"").replaceAll("[\\p{Z}\\s]", " ").trim(); |
String | quoteExecutable(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; |
String | quoteFileName(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 + '"'; ... |
String | quoteForBatchScript(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(); |
String | quoteForCommandString(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(); |
String | quoteForCsv(final String field) Inserts quotation marks around a field if it contains commas if (field != null && field.contains(",")) { return "\"" + field + "\""; return field; |
String | quoteForHTML(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(); |