Java String Unescape unescape(final String string, final char quoteChar)

Here you can find the source of unescape(final String string, final char quoteChar)

Description

Unescapes specified quote character.

License

Open Source License

Parameter

Parameter Description
string string
quoteChar quote character

Return

unescaped string

Declaration

@SuppressWarnings("AssignmentToForLoopParameter")
private static String unescape(final String string, final char quoteChar) 

Method Source Code

//package com.java2s;
/**/*from w ww  .jav  a2  s .c  om*/
 * Copyright 2012 Miroslav ?ulc
 *
 * Permission is hereby granted, free of charge, to any person obtaining a copy
 * of this software and associated documentation files (the "Software"), to deal
 * in the Software without restriction, including without limitation the rights
 * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
 * copies of the Software, and to permit persons to whom the Software is
 * furnished to do so, subject to the following conditions:
 *
 * The above copyright notice and this permission notice shall be included in
 * all copies or substantial portions of the Software.
 *
 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
 * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
 * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
 * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
 * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
 * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
 * SOFTWARE.
 */

public class Main {
    /**
     * Unescapes specified quote character. Bot double quote and backslash
     * escapes are supported.
     *
     * @param string    string
     * @param quoteChar quote character
     *
     * @return unescaped string
     */
    @SuppressWarnings("AssignmentToForLoopParameter")
    private static String unescape(final String string, final char quoteChar) {
        final StringBuilder sbString = new StringBuilder(string.length());
        boolean escaped = false;

        for (int i = 0; i < string.length(); i++) {
            final char chr = string.charAt(i);

            if (chr == '\\') {
                if (i + 1 < string.length() && string.charAt(i + 1) == quoteChar) {
                    i++;
                    sbString.append(quoteChar);
                } else {
                    sbString.append(chr);
                }
            } else if (chr == quoteChar) {
                if (escaped) {
                    sbString.append(chr);
                }

                escaped = !escaped;
            } else {
                sbString.append(chr);
            }
        }

        return sbString.toString();
    }
}

Related

  1. unescape(final String input)
  2. unescape(final String name)
  3. unescape(final String s)
  4. unescape(final String source)
  5. unescape(final String str, final char escapeChar)
  6. unescape(final String text)
  7. unescape(final String[] inval, final Character escapeChar)
  8. unescape(String aString)
  9. unescape(String aValue)