Java String Dequote dequote(String str, char quote)

Here you can find the source of dequote(String str, char quote)

Description

Undoubles the quotes inside the string
Example:
 hello""world becomes hello"world 

License

Apache License

Parameter

Parameter Description
str input string to dequote
quote the quoting char

Return

dequoted string

Declaration

public static String dequote(String str, char quote) 

Method Source Code

//package com.java2s;
/*/*from  ww  w. jav a2s. c o m*/
 * Licensed to the Apache Software Foundation (ASF) under one
 * or more contributor license agreements.  See the NOTICE file
 * distributed with this work for additional information
 * regarding copyright ownership.  The ASF licenses this file
 * to you 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 {
    /**
     * Undoubles the quotes inside the string <br> Example:<br>
     * <pre>
     * hello""world becomes hello"world
     * </pre>
     *
     * @param str input string to dequote
     * @param quote the quoting char
     *
     * @return dequoted string
     */
    public static String dequote(String str, char quote) {
        // Is there anything to dequote?
        if (str == null) {
            return null;
        }

        return dequote(str, 0, str.length(), quote);
    }

    /**
     * Undoubles the quotes inside a substring <br> Example:<br>
     * <pre>
     * hello""world becomes hello"world
     * </pre>
     * WARNING: scan for quote may continue to the end of the string, make sure
     * that either <code>charAt(end + 1) == quote</code> or <code>end =
     * str.lentgth()</code>. If in doubt call
     * <code>dequote(str.substring(begin, end), quote)</code>
     *
     * @param str input string from which to get the substring, must not be
     *        null
     * @param begin begin index for substring
     * @param end end index for substring
     * @param quote the quoting char
     *
     * @return dequoted string
     *
     * @throws IllegalArgumentException if string is incorrectly quoted
     */
    public static String dequote(String str, int begin, int end, char quote) {
        // Is there anything to dequote?
        if (begin == end) {
            return "";
        }

        int endInt = str.indexOf(quote, begin);

        // If no quotes, return the original string
        // and save StringBuilder allocation/char copying
        if (endInt < 0) {
            return str.substring(begin, end);
        }

        StringBuilder sb = new StringBuilder(end - begin);
        int beginInt = begin; // need begin later
        do {
            if (((endInt + 1) >= end) || (str.charAt(endInt + 1) != quote)) {
                throw new IllegalArgumentException(
                        "Internal quote not doubled in string '" + str.substring(begin, end) + "'");
            }

            sb.append(substring(str, beginInt, endInt)).append(quote);
            beginInt = endInt + 2;
            endInt = str.indexOf(quote, beginInt);
        } while ((endInt >= 0) && (endInt < end));

        return sb.append(substring(str, beginInt, end)).toString();
    }

    public static String substring(String str, int begin, int end) {
        if (begin == end) {
            return "";
        }

        return str.substring(begin, end);
    }
}

Related

  1. dequote(String s)
  2. deQuote(String s)
  3. dequote(String str)
  4. dequote(String str)
  5. dequote(String str)
  6. dequote(String string)
  7. dequote(String text)
  8. dequote(String value)
  9. dequote(String value)