Java汉字转拼音工具类

 Java汉字转拼音的类。使用pinyin4j实现

 

package com.pinyin4j;

import net.sourceforge.pinyin4j.PinyinHelper;
import net.sourceforge.pinyin4j.format.HanyuPinyinCaseType;
import net.sourceforge.pinyin4j.format.HanyuPinyinOutputFormat;
import net.sourceforge.pinyin4j.format.HanyuPinyinToneType;

/**
 * 汉字转拼音工具类<br>
 * 基于 pinyin4j
 * 
 * @author Jayden
 * 
 */
public class PinyinUtil {

	private static PinyinUtil instance;

	public static PinyinUtil getInstance() {
		if (instance == null) {
			instance = new PinyinUtil();
		}
		return instance;
	}

	private HanyuPinyinOutputFormat outputFormat = null;

	private HanyuPinyinOutputFormat getOutputFormat() {
		if (outputFormat == null) {
			outputFormat = new HanyuPinyinOutputFormat();
			outputFormat.setToneType(HanyuPinyinToneType.WITHOUT_TONE);
			outputFormat.setCaseType(HanyuPinyinCaseType.LOWERCASE);
		}
		return outputFormat;
	}

	/**
	 * 获取字符串中所有字符首字母
	 * 
	 * @param str
	 * @return
	 * @throws Exception
	 */
	public String getStringInitial(String str) {
		StringBuffer sbf = new StringBuffer();
		if (str != null) {
			for (int i = 0; i < str.length(); i++) {
				sbf.append(getCharInitial(str.charAt(i)));
			}
		}
		return sbf.toString();
	}

	/**
	 * 获取中文字符首字母
	 * 
	 * @param c
	 * @return
	 * @throws Exception
	 */
	public String getCharInitial(char c) {
		try {
			String[] strs = PinyinHelper.toHanyuPinyinStringArray(c,
					getOutputFormat());
			String initial = "";
			if (strs != null && strs.length > 0) {
				String str = strs[0];
				if (str != null && str.length() > 0) {
					initial += str.charAt(0);
				}
			}
			return initial;
		} catch (Exception ex) {
			ex.printStackTrace();
			return c + "";
		}
	}

	public String getStringPinyin(String chines) {

		char[] nameChar = chines.toCharArray();
		String pinyinStr = "";
		for (int i = 0; i < nameChar.length; i++) {
			try {
				char cha=nameChar[i];
				if (nameChar[i] > 128) {
					pinyinStr += PinyinHelper.toHanyuPinyinStringArray(cha, getOutputFormat())[0];
				}
			} catch (Exception ex) {
				ex.printStackTrace();
				pinyinStr += nameChar[i];
			}
		}
		return pinyinStr;
	}

	/**
	 * 获取字符全拼
	 * 
	 * @param c
	 * @return
	 */
	public String getCharPinyin(char c) {

		try {
			String[] strs = PinyinHelper.toHanyuPinyinStringArray(c,
					getOutputFormat());
			String str = "";
			if (strs != null && strs.length > 0) {
				str = strs[0];
			}
			return str;
		} catch (Exception ex) {
			ex.printStackTrace();
			return c + "";
		}
	}

	public static void main(String[] args) {
		String stringInitial = PinyinUtil.getInstance().getStringInitial("玥是个生僻字一般的拼音码解析不了");
		System.out.println(stringInitial);

		String stringPinyin = PinyinUtil.getInstance().getStringPinyin("玥是个生僻字一般的拼音码解析不了");
		System.out.println(stringPinyin);

	}
}


 

  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值