Java String Unquote unquote(final CharSequence quotePhrase)

Here you can find the source of unquote(final CharSequence quotePhrase)

Description

Unquote the quote phrase.

License

Open Source License

Parameter

Parameter Description
quotePhrase the given quote phrase.

Return

the phare.

Declaration

public static CharSequence unquote(final CharSequence quotePhrase) 

Method Source Code

//package com.java2s;
/*****************************************************************
 Copyright 2006 by Dung Nguyen (dungnguyen@truthinet.com)

 Licensed under the iNet Solutions Corp.,;
 you may not use this file except in compliance with the License.
 You may obtain a copy of the License at

 http://www.truthinet.com/licenses/*from   w  w w . java 2  s .c o  m*/

 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 {
    /**
     * Unquote the quote phrase.
     * 
     * <pre>
     * This implementation check the given phrase is quote phrase before removing the quote value.
     * The quote phrase is the phrase has leading and ending with single or double quote character.
     * </pre>
     * 
     * @param quotePhrase the given quote phrase.
     * @return the phare.
     */
    public static CharSequence unquote(final CharSequence quotePhrase) {
        if (quotePhrase.length() == 0)
            return quotePhrase;
        int length = quotePhrase.length();

        // check the valid quote phrase.
        if ((quotePhrase.charAt(0) == '"' && quotePhrase.charAt(length - 1) == '"')
                || (quotePhrase.charAt(0) == '\'' && quotePhrase
                        .charAt(length - 1) == '\'')) {
            // unquote the phrase.
            final StringBuilder builder = new StringBuilder();
            char ch = '\0';

            for (int index = 1; index < length - 1; index++) {
                ch = quotePhrase.charAt(index);
                if (ch == '\\') {
                    builder.append(quotePhrase.charAt(++index));
                } else {
                    builder.append(ch);
                }
            }

            return builder.toString();
        } else {
            // the quote phrase is in
            return quotePhrase;
        }
    }
}

Related

  1. unquote(char[] ca, int pos, StringBuffer out)
  2. unquote(final String aInput)
  3. unquote(final String in)
  4. unQuote(final String quoted)
  5. unquote(final String s)