Java String Quote quoteIdentifier(String s)

Here you can find the source of quoteIdentifier(String s)

Description

Contributed by Thomas Mueller to handle doubling quote characters found in an identifier.

License

Open Source License

Parameter

Parameter Description
s the string to have embedded quotes expanded.

Return

a new string with any embedded quotes doubled, or null if null is passed.

Declaration

public static String quoteIdentifier(String s) 

Method Source Code

//package com.java2s;
/*/*from ww  w  .  j a v  a  2  s  .  com*/
 * Copyright (C) 2007 Rob Manning
 * manningr@users.sourceforge.net
 *
 * This library is free software; you can redistribute it and/or
 * modify it under the terms of the GNU Lesser General Public
 * License as published by the Free Software Foundation; either
 * version 2.1 of the License, or (at your option) any later version.
 *
 * This library is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
 * Lesser General Public License for more details.
 *
 * You should have received a copy of the GNU Lesser General Public
 * License along with this library; if not, write to the Free Software
 * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
 */

public class Main {
    /**
     * Contributed by Thomas Mueller to handle doubling quote characters found in an identifier. In H2 and
     * other dbs, the following statement creates a table with an embedded quote character: CREATE TABLE
     * "foo""bar" (someid int); However, what is returned by the driver for table name is: foo"bar The reason
     * is simple. Just like embedded quotes in SQL strings, such as: select 'I don''t know' from test
     * Similarly, embedded quote characters can also appear in identifiers such as table names, by doubling (or
     * quoting, if you will) the quote.
     * 
     * @param s
     *           the string to have embedded quotes expanded.
     * @return a new string with any embedded quotes doubled, or null if null is passed.
     * @author Thomas Mueller
     */
    public static String quoteIdentifier(String s) {
        if (s == null) {
            return null;
        }
        StringBuilder buff = null;
        buff = new StringBuilder();
        for (int i = 0; i < s.length(); i++) {
            char c = s.charAt(i);
            if (c == '"' && i != 0 && i != s.length() - 1) {
                buff.append(c);
            }
            buff.append(c);
        }
        String result = buff.toString();
        return result;
    }
}

Related

  1. quoteHtmlContent(String x)
  2. quoteIdentifier(String identifier)
  3. quoteIdentifier(String identifier, boolean isPedantic)
  4. quoteIdentifier(String identifier, boolean isPedantic)
  5. quoteIdentifier(String identifier, String quoteChar)
  6. quoteIdentifier(String s)
  7. quoteIfCeylonKeyword(String name)
  8. quoteIfNeeded(String id)
  9. quoteIfNeeded(String input)