Java String Quote quote(String toQuote, String specials, char quoteChar)

Here you can find the source of quote(String toQuote, String specials, char quoteChar)

Description

Quote special characters.

License

Open Source License

Parameter

Parameter Description
toQuote The String which may contain special characters.
specials A String containing all special characters except the quoting character itself, which is automatically quoted.
quoteChar The quoting character.

Return

A String with every special character (including the quoting character itself) quoted.

Declaration

public static String quote(String toQuote, String specials, char quoteChar) 

Method Source Code

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

public class Main {
    /**//from www  .ja va2  s  .  c  om
     * Quote special characters.
     *
     * @param toQuote         The String which may contain special characters.
     * @param specials  A String containing all special characters except the quoting
     *                  character itself, which is automatically quoted.
     * @param quoteChar The quoting character.
     * @return A String with every special character (including the quoting
     * character itself) quoted.
     */
    public static String quote(String toQuote, String specials, char quoteChar) {
        if (toQuote == null) {
            return "";
        }

        StringBuilder result = new StringBuilder();
        char c;
        boolean isSpecial;
        for (int i = 0; i < toQuote.length(); ++i) {
            c = toQuote.charAt(i);

            isSpecial = (c == quoteChar);
            // If non-null specials performs logic-or with specials.indexOf(c) >= 0
            isSpecial |= ((specials != null) && (specials.indexOf(c) >= 0));

            if (isSpecial) {
                result.append(quoteChar);
            }
            result.append(c);
        }
        return result.toString();
    }
}

Related

  1. quote(String text)
  2. quote(String text)
  3. quote(String text)
  4. quote(String text)
  5. quote(String text, String quote)
  6. quote(String txt)
  7. quote(String val)
  8. quote(String val)
  9. quote(String val, char quote)