Java String Unquote unQuoteIdentifier(String identifier, String quoteChar)

Here you can find the source of unQuoteIdentifier(String identifier, String quoteChar)

Description

Trims identifier, removes quote chars from first and last positions and replaces double occurrences of quote char from entire identifier, i.e converts quoted identifier into form as it is stored in database.

License

Apache License

Parameter

Parameter Description
identifier a parameter
quoteChar ` or "

Return

  • null -> null
  • abc -> abc
  • `abc` -> abc
  • `ab``c` -> ab`c
  • `"ab`c"` -> "ab`c"
  • `ab"c` -> ab"c
  • "abc" -> abc
  • "`ab""c`" -> `ab"c`
  • "ab`c" -> ab`c

Declaration

public static String unQuoteIdentifier(String identifier, String quoteChar) 

Method Source Code

//package com.java2s;
/*/*from w w w. ja v a  2  s  .c o m*/
 * Copyright 2017 Google Inc.
 * 
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 * 
 *     http://www.apache.org/licenses/LICENSE-2.0
 * 
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */

public class Main {
    /**
     * Trims identifier, removes quote chars from first and last positions
     * and replaces double occurrences of quote char from entire identifier,
     * i.e converts quoted identifier into form as it is stored in database.
     *
     * @param identifier
     * @param quoteChar
     *            ` or "
     * @return
     *         <ul>
     *         <li>null -> null</li>
     *         <li>abc -> abc</li>
     *         <li>`abc` -> abc</li>
     *         <li>`ab``c` -> ab`c</li>
     *         <li>`"ab`c"` -> "ab`c"</li>
     *         <li>`ab"c` -> ab"c</li>
     *         <li>"abc" -> abc</li>
     *         <li>"`ab""c`" -> `ab"c`</li>
     *         <li>"ab`c" -> ab`c</li>
     *         </ul>
     */
    public static String unQuoteIdentifier(String identifier, String quoteChar) {
        if (identifier == null) {
            return null;
        }
        identifier = identifier.trim();
        int quoteCharLength = quoteChar.length();
        if (quoteCharLength == 0 || " ".equals(quoteChar)) {
            return identifier;
        }

        // Check if the identifier is really quoted or if it simply contains quote chars in it (assuming that the value is a valid identifier).
        if (identifier.startsWith(quoteChar) && identifier.endsWith(quoteChar)) {
            String identifierQuoteTrimmed = identifier.substring(quoteCharLength,
                    identifier.length() - quoteCharLength);
            // Check for pairs of quotes.
            int quoteCharPos = identifierQuoteTrimmed.indexOf(quoteChar);
            while (quoteCharPos >= 0) {
                int quoteCharNextExpectedPos = quoteCharPos + quoteCharLength;
                int quoteCharNextPosition = identifierQuoteTrimmed.indexOf(quoteChar, quoteCharNextExpectedPos);

                if (quoteCharNextPosition == quoteCharNextExpectedPos) {
                    quoteCharPos = identifierQuoteTrimmed.indexOf(quoteChar,
                            quoteCharNextPosition + quoteCharLength);
                } else {
                    // Not a pair of quotes! Return as it is...
                    return identifier;
                }
            }
            return identifier.substring(quoteCharLength, (identifier.length() - quoteCharLength))
                    .replaceAll(quoteChar + quoteChar, quoteChar);
        }
        return identifier;
    }
}

Related

  1. unquoteCString(String str)
  2. unquotedCharLiteral(char c)
  3. unquotedString(String str)
  4. unquoteHtmlContent(String x)
  5. unQuoteIdentifier(String identifier, boolean useAnsiQuotedIdentifiers)
  6. unQuoteIdentifier(String identifier, String quoteChar)
  7. unquoteImpl(String s, char delim)
  8. unquotePath(String path)
  9. unquoteS(String s)