Java String Quote quoteIdentifier(String identifier, String quoteChar)

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

Description

Quotes an identifier, escaping any dangling quotes within

License

Apache License

Parameter

Parameter Description
identifier a parameter
quoteChar a parameter

Declaration

public static String quoteIdentifier(String identifier, String quoteChar) 

Method Source Code

//package com.java2s;
/*/* ww w .  j  a  v a  2 s  .c  om*/
 * 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 {
    /**
     * Quotes an identifier, escaping any dangling quotes within
     * @param identifier
     * @param quoteChar
     * @return
     */
    public static String quoteIdentifier(String identifier, String quoteChar) {
        if (identifier == null) {
            return null;
        }
        identifier = identifier.trim();
        int quoteCharLength = quoteChar.length();
        if (quoteCharLength == 0 || " ".equals(quoteChar)) {
            return identifier;
        }

        if (identifier.startsWith(quoteChar) && identifier.endsWith(quoteChar)) {
            String identifierQuoteTrimmed = identifier.substring(quoteCharLength,
                    identifier.length() - quoteCharLength);
            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!
                    break;
                }
            }

            if (quoteCharPos < 0) {
                return identifier;
            }
        }

        return quoteChar + identifier.replaceAll(quoteChar, quoteChar + quoteChar) + quoteChar;
    }
}

Related

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