Java Unicode Unescape unescapeUnicode(String s)

Here you can find the source of unescapeUnicode(String s)

Description

Unescapes unicode sequences and stores them as unicode characters instead.

License

Open Source License

Parameter

Parameter Description
s the string to process

Return

the unescaped string

Declaration

public static String unescapeUnicode(String s) 

Method Source Code

//package com.java2s;
/*/*w  ww. ja v a2  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 as published by
 *   the Free Software Foundation, either version 3 of the License, or
 *   (at your option) any later version.
 *
 *   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.
 *
 *   You should have received a copy of the GNU General Public License
 *   along with this program.  If not, see <http://www.gnu.org/licenses/>.
 */

public class Main {
    /**
     * Unescapes unicode sequences and stores them as unicode characters instead.
     *
     * @param s        the string to process
     * @return      the unescaped string
     */
    public static String unescapeUnicode(String s) {
        StringBuilder result;
        int index;
        String unicode;
        char[] chars;

        if (!s.contains("\\u"))
            return s;

        result = new StringBuilder();
        while ((index = s.indexOf("\\u")) > -1) {
            result.append(s.substring(0, index));
            s = s.substring(index + 2);
            if (s.length() >= 4) {
                unicode = s.substring(0, 4).toUpperCase();
                s = s.substring(4);
                chars = Character.toChars(Integer.parseInt(unicode, 16));
                for (char c : chars)
                    result.append(c);
            } else {
                result.append("\\u");
            }
        }
        result.append(s);

        return result.toString();
    }
}

Related

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