Java String Unquote unquote(String maybeQuoted)

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

Description

Removes single or double quotes which surround a String.

License

Open Source License

Parameter

Parameter Description
maybeQuoted A String which may or may not be surrounded with single or double quotes.

Return

The input String with zero or one pairs of surrounding quotes removed, or null if the input String is null.

Declaration

public static String unquote(String maybeQuoted) 

Method Source Code

//package com.java2s;
//License from project: Open Source License 

public class Main {
    /**//w  ww  .j  a  va 2  s  . c  om
     * Removes single or double quotes which surround a String.  Matching quotes must appear at the
     * beginning and end of the String for it to be considered quoted.
     * @param maybeQuoted A String which may or may not be surrounded with single or double quotes.
     * @return The input String with zero or one pairs of surrounding quotes removed, or null if the input String is null.
     * @author: Sam Barnum
     */
    public static String unquote(String maybeQuoted) {
        if (maybeQuoted == null || maybeQuoted.length() < 2)
            return maybeQuoted;
        boolean isQuoted = false;
        int len = maybeQuoted.length();
        if (maybeQuoted.charAt(0) == '"' && maybeQuoted.charAt(len - 1) == '"')
            isQuoted = true;
        else if (maybeQuoted.charAt(0) == '\'' && maybeQuoted.charAt(len - 1) == '\'')
            isQuoted = true;
        if (isQuoted) {
            maybeQuoted = maybeQuoted.substring(1, len - 1);
        }
        return maybeQuoted;
    }
}

Related

  1. unquote(String aString)
  2. unquote(String entityName)
  3. unquote(String in)
  4. unquote(String input, char quoteChar)
  5. unquote(String literal)
  6. unquote(String message)
  7. unquote(String name)
  8. unquote(String name)
  9. unquote(String quoted)