Java Unicode Unescape unescapeUnicodeChars(String string)

Here you can find the source of unescapeUnicodeChars(String string)

Description

Unescapes \\uXXXX encoded chars.

License

Open Source License

Declaration

public static String unescapeUnicodeChars(String string) 

Method Source Code

//package com.java2s;
/**// w w w  . j  a v  a2s.  c  o  m
 * This file is part of tapioca.core.
 *
 * tapioca.core 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 3 of the License, or
 * (at your option) any later version.
 *
 * tapioca.core 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 tapioca.core.  If not, see <http://www.gnu.org/licenses/>.
 */

public class Main {
    /**
     * Unescapes \\uXXXX encoded chars.
     */
    public static String unescapeUnicodeChars(String string) {
        int pos = string.indexOf("\\u");
        if (pos < 0) {
            return string;
        }
        StringBuilder newString = new StringBuilder();
        int charValue, oldPos = 0;
        while ((pos >= 0) && (pos + 5 < string.length())) {
            newString.append(string.substring(oldPos, pos));
            try {
                charValue = Integer.parseInt(string.substring(pos + 2, pos + 6), 16);
                newString.append(Character.toChars(charValue));
                oldPos = pos + 6;
            } catch (Exception e) {
                // couldn't extract a number --> seems that the \\u is no encoded unicode
                oldPos = pos + 2;
            }
            pos = string.indexOf("\\u", pos + 2);
        }
        return newString.toString();
    }
}

Related

  1. unescapeHTMLUnicodeEntity(final String text)
  2. unescapeUnicode(String encoded)
  3. unescapeUnicode(String s)
  4. unescapeUnicode(String s)