unicode文字列変換文字

1250 ワード

package com.yt17lu.commons;

import java.util.Scanner;

public class UnicodeParse {
	/**
	 *
	 * @param dataStr
	 *             unicode  
	 * @return  
	 * @throws  u 2-4 16 
	 */
	public static String parse(String dataStr) {
		boolean matches = dataStr.matches("^(\\\\u\\p{XDigit}{2,4})*$");
		if (!matches) {
			throw new RuntimeException(dataStr + ",unicode ");
		}
		int start = 0;
		int end = 0;
		StringBuffer buffer = new StringBuffer();
		int len = dataStr.length();
		while (start < len) {
			end = dataStr.indexOf("\\u", start + 2);
			end = (end == -1) ? len : end;
			String temp = dataStr.substring(start + 2, end);
			buffer.append((char) Integer.parseInt(temp, 16));
			start = end;
		}
		return buffer.toString();
	}

	public static void main(String[] args) {
		Scanner input = new Scanner(System.in);
		System.out.println(" :");
		String code = input.next();
		System.out.println(UnicodeParse.parse(code));
	}
}