Decode input Unicode style String, return the original string with regex - Java java.lang

Java examples for java.lang:String Unicode

Description

Decode input Unicode style String, return the original string with regex

Demo Code


//package com.java2s;
import java.util.regex.Matcher;
import java.util.regex.Pattern;

public class Main {
    /**//from  w w w  .j  a v a2s  . c  om
     * <p>Decode input Unicode style String, return the original string</p>
     * @param unicode
     * @return 
     * @since 1.0
     */
    public static String decode(String unicode) {
        Pattern pattern = Pattern.compile("(\\\\u(\\p{XDigit}{4}))");
        Matcher matcher = pattern.matcher(unicode);
        char ch;
        while (matcher.find()) {
            ch = (char) Integer.parseInt(matcher.group(2), 16);
            unicode = unicode.replace(matcher.group(1), ch + "");
        }
        return unicode;
    }
}

Related Tutorials