Here you can find the source of unQuoteIdentifier(String identifier, String quoteChar)
Parameter | Description |
---|---|
identifier | a parameter |
quoteChar | ` or " |
public static String unQuoteIdentifier(String identifier, String quoteChar)
//package com.java2s; /*/*from w ww. j av a 2 s . co m*/ Copyright (c) 2002, 2016, Oracle and/or its affiliates. All rights reserved. The MySQL Connector/J is licensed under the terms of the GPLv2 <http://www.gnu.org/licenses/old-licenses/gpl-2.0.html>, like most MySQL Connectors. There are special exceptions to the terms and conditions of the GPLv2 as it is applied to this software, see the FOSS License Exception <http://www.mysql.com/about/legal/licensing/foss-exception.html>. This program is free software; you can redistribute it and/or modify it under the terms of the GNU General Public License as published by the Free Software Foundation; version 2 of the License. This program 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 General Public License for more details. You should have received a copy of the GNU General Public License along with this program; if not, write to the Free Software Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA */ 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 {@code ->} null</li> * <li>abc {@code ->} abc</li> * <li>`abc` {@code ->} abc</li> * <li>`ab``c` {@code ->} ab`c</li> * <li>`"ab`c"` {@code ->} "ab`c"</li> * <li>`ab"c` {@code ->} ab"c</li> * <li>"abc" {@code ->} abc</li> * <li>"`ab""c`" {@code ->} `ab"c`</li> * <li>"ab`c" {@code ->} 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) { 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)) { // Trim outermost quotes from the identifier. 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; } private static boolean startsWith(byte[] dataFrom, String chars) { int charsLength = chars.length(); if (dataFrom.length < charsLength) { return false; } for (int i = 0; i < charsLength; i++) { if (dataFrom[i] != chars.charAt(i)) { return false; } } return true; } private static boolean endsWith(byte[] dataFrom, String suffix) { for (int i = 1; i <= suffix.length(); i++) { int dfOffset = dataFrom.length - i; int suffixOffset = suffix.length() - i; if (dataFrom[dfOffset] != suffix.charAt(suffixOffset)) { return false; } } return true; } public static int indexOf(byte[] s, char c) { if (s == null) { return -1; } int length = s.length; for (int i = 0; i < length; i++) { if (s[i] == c) { return i; } } return -1; } }