Java String Unquote unquoteString(String value)

Here you can find the source of unquoteString(String value)

Description

If the value is an HTTP quoted-string then we strip of the quote characters now and translate any escaped characters into themselves, e.g., '\"' => '"'.

License

Open Source License

Parameter

Parameter Description
IllegalArgumentException If the value is a malformed quoted string. For example, no closing quote character or no character after an escape character.

Declaration


static public String unquoteString(String value) throws IllegalArgumentException 

Method Source Code

//package com.java2s;
/**/*  w  ww .j a  v  a  2  s . c  om*/
    
Copyright (C) SYSTAP, LLC 2006-2011.  All rights reserved.
    
Contact:
 SYSTAP, LLC
 4501 Tower Road
 Greensboro, NC 27410
 licenses@bigdata.com
    
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., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
*/

public class Main {
    /**
     * If the value is an HTTP <code>quoted-string</code> then we
     * strip of the quote characters now and translate any escaped
     * characters into themselves, e.g., '\"' => '"'.  Otherwise
     * returns <i>value</i>.
     *
     * @param IllegalArgumentException If the value is a malformed
     * quoted string.  For example, no closing quote character or no
     * character after an escape character.
     */

    static public String unquoteString(String value) throws IllegalArgumentException {

        String originalValue = value; // save.

        /* Do nothing unless a quoted-string.
         */

        if (!value.startsWith("\"")) {

            return value;

        }

        /* Drop off the quote characters.
         */

        if (!value.endsWith("\"")) {

            throw new IllegalArgumentException("Quoted string does not end with '\"'" + " : " + originalValue);

        }

        // Chop off the quote characters.

        value = value.substring(1, value.length() - 1);

        // Translate escaped characters.

        StringBuffer sb = new StringBuffer();

        int len = value.length();

        for (int i = 0; i < len; i++) {

            char ch = value.charAt(i);

            if (ch == '\\') {

                i++;

                if (i < len) {

                    ch = value.charAt(i);

                    sb.append(ch);

                } else {

                    throw new IllegalArgumentException("Escape character at end of string" + " : " + originalValue);

                }

            } else {

                sb.append(ch);

            }

        }

        return sb.toString();

    }
}

Related

  1. unquoteS(String s)
  2. unquoteString(final String input)
  3. unquoteString(String s)
  4. unquoteString(String s)
  5. unquoteString(String s)
  6. unquoteText(String s)
  7. unquoteXML(String xmlFragment)