Java String Unescape unescape(final String name)

Here you can find the source of unescape(final String name)

Description

Reverts modifications of #escape(String) such that for all String s t , t.equals(unescape(escape(t))) .

License

Open Source License

Parameter

Parameter Description
name name to unescape

Return

unescaped name

Declaration

public static String unescape(final String name) 

Method Source Code

//package com.java2s;
/*/* w  w w.  ja v  a 2 s .co m*/
 * This program is free software; you can redistribute it and/or modify it under the
 * terms of the GNU General Public License, version 2 as published by the Free Software
 * Foundation.
 *
 * You should have received a copy of the GNU General Public License along with this
 * program; if not, you can obtain a copy at http://www.gnu.org/licenses/gpl-2.0.html
 * or from the Free Software Foundation, Inc.,
 * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
 *
 * This program 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 General Public License for more details.
 *
 *
 * Copyright 2006 - 2016 Pentaho Corporation.  All rights reserved.
 */

public class Main {
    private static Character escapeChar;

    /**
     * Reverts modifications of {@link #escape(String)} such that for all {@code String}s {@code t},
     * {@code t.equals(unescape(escape(t)))}. Assumes only ASCII characters have been escaped.
     * 
     * @param name
     *          name to unescape
     * @return unescaped name
     */
    public static String unescape(final String name) {
        if (name == null) {
            throw new IllegalArgumentException();
        }
        StringBuilder buffer = new StringBuilder(name.length());
        String str = name;
        int i = str.indexOf(escapeChar);
        while (i > -1 && i + 2 < str.length()) {
            buffer.append(str.toCharArray(), 0, i);
            int a = Character.digit(str.charAt(i + 1), 16);
            int b = Character.digit(str.charAt(i + 2), 16);
            if (a > -1 && b > -1) {
                buffer.append((char) (a * 16 + b));
                str = str.substring(i + 3);
            } else {
                buffer.append(escapeChar);
                str = str.substring(i + 1);
            }
            i = str.indexOf(escapeChar);
        }
        buffer.append(str);
        return buffer.toString();
    }
}

Related

  1. unescape(final CharSequence target)
  2. unescape(final String aSpecification)
  3. unescape(final String input)
  4. unescape(final String s)
  5. unescape(final String source)
  6. unescape(final String str, final char escapeChar)
  7. unescape(final String string, final char quoteChar)