Java String Unquote unquote(String str)

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

Description

Dequotifies a specified string.

License

Open Source License

Parameter

Parameter Description
str the string to dequotify.

Return

unquotified string.

Declaration

public static String unquote(String str) throws Exception 

Method Source Code

//package com.java2s;
/*/*from   www.j  a  va 2 s  .c  o m*/
 * Portions of this file Copyright 1999-2005 University of Chicago
 * Portions of this file Copyright 1999-2005 The University of Southern California.
 *
 * This file or a portion of this file is licensed under the
 * terms of the Globus Toolkit Public License, found at
 * http://www.globus.org/toolkit/download/license.html.
 * If you redistribute this file, with or without
 * modifications, you must include this notice in the file.
 */

public class Main {
    /**
     * Dequotifies a specified string.
     * The quotes are removed and each \" is replaced with " and
     * each \\ is replaced with \.
     *
     * @param str the string to dequotify.
     * @return unquotified string.
     */
    public static String unquote(String str) throws Exception {
        int len = str.length();
        StringBuffer buf = new StringBuffer(len);
        boolean inQuotes = false;
        char c;
        int i = 0;

        if (str.charAt(i) == '"') {
            inQuotes = true;
            i++;
        }
        while (i < len) {
            c = str.charAt(i);
            if (inQuotes) {
                if (c == '"') {
                    inQuotes = false;
                } else if (c == '\\') {
                    buf.append(str.charAt(++i));
                } else {
                    buf.append(c);
                }
            } else {
                if (c == '\r') {
                    if (str.charAt(i++) != '\n') {
                        throw new Exception("Malformed string.");
                    }
                } else if (c == '"') {
                    inQuotes = true;
                    i++;
                } else {
                    buf.append(c);
                }
            }
            i++;
        }
        return buf.toString();
    }
}

Related

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