Java String Dequote dequoteString(String str)

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

Description

Replace quoted characters by their unquoted version.

License

Open Source License

Parameter

Parameter Description
str the string do dequote

Return

the string do with all quoted characters replaced by their true value.

Declaration

public final static String dequoteString(String str) 

Method Source Code

//package com.java2s;
/*/* w  w w  . jav  a2  s  . c  om*/
 * @(#)Util.java             0.3-3 06/05/2001
 *
 *  This file is part of the HTTPClient package
 *  Copyright (C) 1996-2001 Ronald Tschal???r
 *
 *  This library is free software; you can redistribute it and/or
 *  modify it under the terms of the GNU Lesser General Public
 *  License as published by the Free Software Foundation; either
 *  version 2 of the License, or (at your option) any later version.
 *
 *  This library 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
 *  Lesser General Public License for more details.
 *
 *  You should have received a copy of the GNU Lesser General Public
 *  License along with this library; if not, write to the Free
 *  Software Foundation, Inc., 59 Temple Place, Suite 330, Boston,
 *  MA 02111-1307, USA
 *
 *  For questions, suggestions, bug-reports, enhancement-requests etc.
 *  I may be contacted at:
 *
 *  ronald@innovation.ch
 *
 *  The HTTPClient's home page is located at:
 *
 *  http://www.innovation.ch/java/HTTPClient/ 
 *
 */

public class Main {
    /**
     * Replace quoted characters by their unquoted version. Quoted characters are
     * characters preceded by a slash. E.g. "\c" would be replaced by "c". This is
     * used in parsing http headers where quoted-characters are allowed in
     * quoted-strings and often used to quote the quote character <">.
     * @param str the string do dequote
     * @return the string do with all quoted characters replaced by their true
     *         value.
     */
    public final static String dequoteString(String str) {
        if (str.indexOf('\\') == -1)
            return str;

        char[] buf = str.toCharArray();
        int pos = 0, num_deq = 0;
        while (pos < buf.length) {
            if (buf[pos] == '\\' && pos + 1 < buf.length) {
                System.arraycopy(buf, pos + 1, buf, pos, buf.length - pos - 1);
                num_deq++;
            }
            pos++;
        }

        return new String(buf, 0, buf.length - num_deq);
    }
}

Related

  1. dequote(String value)
  2. dequote(String value)
  3. dequoteFull(String str, char quote)
  4. dequoteIdentifier(String id)
  5. dequoteString(String s)
  6. dequoteString(String str)
  7. dequoteString(String strVal)
  8. deQuoteStringArray(String... quoted)