Java String Quote quoteString(final String str)

Here you can find the source of quoteString(final String str)

Description

Surround the string with single quotes, and backquote any single quotes in the string.

License

Open Source License

Parameter

Parameter Description
str the input string

Return

the quoted string

Declaration

public static String quoteString(final String str) 

Method Source Code

//package com.java2s;
//License from project: Open Source License 

public class Main {
    /**/* w  ww  . j  a  v  a 2 s .  c  o  m*/
     * Surround the string with single quotes, and backquote any
     * single quotes in the string.
     * 
     * @param str the input string
     * @return the quoted string
     */
    public static String quoteString(final String str) {
        // Check the input
        if (str == null) {
            // It's null, so just return that
            return "null";
        }

        String outStr = str.replace("\"", "\\\"");

        if (outStr.contains("\n") || outStr.contains(",")) {
            outStr = "\"" + outStr + "\"";
        }

        return outStr;
    }
}

Related

  1. quoteSqlIdentifier(String name)
  2. quoteSQLString(String s)
  3. quoteStr(Object o)
  4. quoteStrictOccurrences(final String textToMarkup, final String filter)
  5. quoteString(final String input)
  6. quoteString(final String string)
  7. quoteString(final String text)
  8. quoteString(String arg)
  9. quoteString(String aTarget, String aPrefix, String aSuffix)