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

StringBuilderquote(StringBuilder sb, String str)
quote
return sb.append('"').append(str).append('"');
Stringquote4CMDLine(String str)
quote CMD Line
if (str == null) {
    return "\"\"";
int len = str.length();
int startPos = 0;
String trans = null;
StringBuffer res = new StringBuffer("\"");
for (int i = 0; i < len; i++) {
...
StringquoteAndClean(String str)
quote And Clean
if (str == null || str.isEmpty()) {
    return "\"\"";
StringBuffer buffer = new StringBuffer(str.length());
buffer.append('"');
for (int i = 0; i < str.length(); ++i) {
    char ch = str.charAt(i);
    switch (ch) {
...
StringquoteAndEscape(String str)
quote And Escape
return quoteAndEscape(new StringBuilder(), str).toString();
StringquoteAndEscapeFilename(String filename)
quote And Escape Filename
if (filename.indexOf(" ") < 0)
    return filename;
return "\"" + filename.replaceAll("\\\\", "\\\\\\\\") + "\"";
StringquoteAndEscapeValue(String serviceQName)
quote And Escape Value
return "\"" + serviceQName.replace("?", REPLACED_QUOTATION_MARK) + "\"";
StringquoteArg(final String arg)
quote Arg
if (arg.indexOf(' ') > -1 || arg.indexOf('\t') > -1 || arg.indexOf('"') > -1) {
    if (arg.charAt(0) != '"' && (arg.charAt(arg.length() - 1) != '"')) {
        StringBuilder sb = new StringBuilder();
        sb.append("\""); 
        sb.append(arg.replace("\"", "\"\"")); 
        sb.append("\""); 
        return sb.toString();
return arg;
StringquoteArg(String arg)
Returns the argument string surrounded with quotes if it contains a space, otherwise returns the string as is.
if (arg != null && arg.indexOf(' ') > -1) {
    return "\"" + arg + "\"";
return arg;
StringquoteArgForCommand(String input)
Quote a string suitable for the "quote aware" parser of ToHCommandExecutor .
input = input.replaceAll("\\\\", "\\\\\\\\").replaceAll("\"", "\\\\\"");
if (input.matches(".*\\s.*"))
    return "\"" + input + "\""; 
else
    return input;
StringquoteArgument(final String argument)
Puts quotes around the given String if necessary.
String cleanedArgument = argument.trim();
while (cleanedArgument.startsWith(SINGLE_QUOTE) && cleanedArgument.endsWith(SINGLE_QUOTE)
        || cleanedArgument.startsWith(DOUBLE_QUOTE) && cleanedArgument.endsWith(DOUBLE_QUOTE)) {
    cleanedArgument = cleanedArgument.substring(1, cleanedArgument.length() - 1);
final StringBuilder buf = new StringBuilder();
if (cleanedArgument.contains(DOUBLE_QUOTE)) {
    if (cleanedArgument.contains(SINGLE_QUOTE)) {
...