Java String Unescape unescape(final String str, final char escapeChar)

Here you can find the source of unescape(final String str, final char escapeChar)

Description

Unescapes characters in a escaped string.

License

Apache License

Parameter

Parameter Description
str the escaped string to be unescaped.
escapeChar the character used to escape itself and other characters.

Return

the unescaped string.

Declaration

public static String unescape(final String str, final char escapeChar) 

Method Source Code

//package com.java2s;
/*//w w w. ja  v  a 2 s  . c  o  m
 * Copyright (c) 2014  Haixing Hu
 *
 * Licensed 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 {
    /**
     * Unescapes characters in a escaped string.
     * <p>
     * All characters in the escaped string escaped by the specified escape
     * character will be unescaped.
     *
     * @param str
     *          the escaped string to be unescaped.
     * @param escapeChar
     *          the character used to escape itself and other characters.
     * @return the unescaped string.
     */
    public static String unescape(final String str, final char escapeChar) {
        final StringBuilder builder = new StringBuilder(str.length());
        unescape(str, escapeChar, builder);
        return builder.toString();
    }

    /**
     * Unescapes characters in a escaped string.
     * <p>
     * All characters in the escaped string escaped by the specified escape
     * character will be unescaped.
     *
     * @param str
     *          the escaped string to be unescaped.
     * @param escapeChar
     *          the character used to escape itself and other characters.
     * @param builder
     *          a string builder where to append the unescaped string.
     */
    public static void unescape(final String str, final char escapeChar, final StringBuilder builder) {
        final int n = str.length();
        boolean escaped = false;
        for (int i = 0; i < n; ++i) {
            final char ch = str.charAt(i);
            if (escaped) {
                builder.append(ch);
                escaped = false;
            } else if (ch == escapeChar) {
                escaped = true;
            } else {
                builder.append(ch);
            }
        }
    }
}

Related

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