Java String Unquote unquote(String str)

Here you can find the source of unquote(String str)

Description

Removes single or double quotes from a String

License

Apache License

Parameter

Parameter Description
str the String from which quotes will be removed

Return

the unquoted String

Declaration

public static String unquote(String str) 

Method Source Code

//package com.java2s;
/*//w  w w.  j a v a  2  s  .c  om
 * Copyright 2008-2015 the original author or authors.
 *
 * 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 {
    /**
     * Removes single or double quotes from a String
     *
     * @param str the String from which quotes will be removed
     * @return the unquoted String
     */
    public static String unquote(String str) {
        if (isBlank(str))
            return str;
        if ((str.startsWith("'") && str.endsWith("'")) || (str.startsWith("\"") && str.endsWith("\""))) {
            return str.substring(1, str.length() - 1);
        }
        return str;
    }

    /**
     * <p>Determines whether a given string is <code>null</code>, empty,
     * or only contains whitespace. If it contains anything other than
     * whitespace then the string is not considered to be blank and the
     * method returns <code>false</code>.</p>
     * <p>We could use Commons Lang for this, but we don't want GriffonNameUtils
     * to have a dependency on any external library to minimise the number of
     * dependencies required to bootstrap Griffon.</p>
     *
     * @param str The string to test.
     * @return <code>true</code> if the string is <code>null</code>, or
     * blank.
     */
    public static boolean isBlank(String str) {
        if (str == null || str.length() == 0) {
            return true;
        }
        for (char c : str.toCharArray()) {
            if (!Character.isWhitespace(c)) {
                return false;
            }
        }

        return true;
    }
}

Related

  1. unquote(String s)
  2. unquote(String s)
  3. unquote(String s)
  4. unquote(String s, char c)
  5. unquote(String str)
  6. unquote(String str)
  7. unquote(String str)
  8. unquote(String str)
  9. unquote(String str)