All Downloads are FREE. Search and download functionalities are using the official Maven repository.

com.webapp.utils.jpinyin.ChineseHelper Maven / Gradle / Ivy

There is a newer version: 1.5.0
Show newest version
package com.webapp.utils.jpinyin;

import java.util.Iterator;
import java.util.Map.Entry;
import java.util.Properties;

public final class ChineseHelper {
	private static final Properties CHINESETABLE = PinyinResource.getChineseTable();

	/**
	 * 将单个繁体字转换为简体字
	 *
	 * @param c
	 *            需要转换的繁体字
	 * @return 转换后的简体字
	 */
	public static char convertToSimplifiedChinese(char c) {
		if (isTraditionalChinese(c)) {
			return CHINESETABLE.getProperty(String.valueOf(c)).charAt(0);
		}
		return c;
	}

	/**
	 * 将单个简体字转换为繁体字
	 *
	 * @param c
	 *            需要转换的简体字
	 * @return 转换后的繁字体
	 */
	public static char convertToTraditionalChinese(char c) {
		String hanzi = String.valueOf(c);
		if (CHINESETABLE.containsValue(hanzi)) {
			Iterator> itr = CHINESETABLE.entrySet().iterator();
			while (itr.hasNext()) {
				Entry e = itr.next();
				if (e.getValue().toString().equals(hanzi)) {
					return e.getKey().toString().charAt(0);
				}
			}
		}
		return c;
	}

	/**
	 * 将繁体字转换为简体字
	 *
	 * @param str
	 *            需要转换的繁体字
	 * @return 转换后的简体体
	 */
	public static String convertToSimplifiedChinese(String str) {
		StringBuilder sb = new StringBuilder();
		for (int i = 0, len = str.length(); i < len; i++) {
			char c = str.charAt(i);
			sb.append(convertToSimplifiedChinese(c));
		}
		return sb.toString();
	}

	/**
	 * 将简体字转换为繁体字
	 *
	 * @param str
	 *            需要转换的简体字
	 * @return 转换后的繁字体
	 */
	public static String convertToTraditionalChinese(String str) {
		StringBuilder sb = new StringBuilder();
		for (int i = 0, len = str.length(); i < len; i++) {
			char c = str.charAt(i);
			sb.append(convertToTraditionalChinese(c));
		}
		return sb.toString();
	}

	/**
	 * 判断某个字符是否为繁体字
	 *
	 * @param c
	 *            需要判断的字符
	 * @return 是繁体字返回true,否则返回false
	 */
	public static boolean isTraditionalChinese(char c) {
		return CHINESETABLE.containsKey(String.valueOf(c));
	}

	/**
	 * 判断某个字符是否为汉字
	 *
	 * @param c
	 *            需要判断的字符
	 * @return 是汉字返回true,否则返回false
	 */
	public static boolean isChinese(char c) {
		String regex = "[\\u4e00-\\u9fa5]";
		return String.valueOf(c).matches(regex);
	}
}




© 2015 - 2024 Weber Informatics LLC | Privacy Policy