汉字转拼音工具类

张军 2167 0

Java汉字转成汉语拼音工具类,需要用到pinyin4j.jar包.

张军博客

package zj.pinyin;

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

import org.apache.log4j.Logger;

/**
 * 汉字转拼音工具类
 * 
 * @version 1.00 (2014.09.15)
 * @author SHNKCS 张军 {@link  <a target=_blank href=http://www.zhangjunbk.com>张军个人网站</a>&nbsp;&nbsp;&nbsp;&nbsp;<a target=_blank href=http://user.qzone.qq.com/360901061/>张军QQ空间</a>}
 */
public class PinyinUtil {
	private transient static final Logger logger = Logger.getLogger(PinyinUtil.class);

	/**
	 * 拼音字母
	 * 
	 * @param text
	 *            原字符
	 * @author 张军
	 * @date 2016-2-1 9:16:00
	 * @modifiyNote
	 * @version 1.0
	 * @return 拼音首字母
	 */
	public static String textToPingYin(String text) {
		try {
			char[] t1 = null;
			t1 = text.toCharArray();
			String[] t2 = new String[t1.length];
			HanyuPinyinOutputFormat t3 = new HanyuPinyinOutputFormat();
			t3.setCaseType(HanyuPinyinCaseType.LOWERCASE);
			t3.setToneType(HanyuPinyinToneType.WITHOUT_TONE);
			t3.setVCharType(HanyuPinyinVCharType.WITH_V);
			int t0 = t1.length;
			String t4 = "";
			for (int i = 0; i < t0; i++) {
				// 判断是否为汉字字符
				if (java.lang.Character.toString(t1[i]).matches("[\\u4E00-\\u9FA5]+")) {
					t2 = PinyinHelper.toHanyuPinyinStringArray(t1[i], t3);
					t4 += t2[0];
				} else {
					t4 += java.lang.Character.toString(t1[i]);
				}
			}
			return t4;
		} catch (Exception e) {
			logger.debug("转换拼音出错,返回原字符", e);
			return text;
		}
	}

	/**
	 * 拼音首字母
	 * 
	 * @param text
	 *            原字符
	 * @author 张军
	 * @date 2016-2-1 9:16:00
	 * @modifiyNote
	 * @version 1.0
	 * @return 拼音首字母
	 */
	public static String textToPinYinHeadChar(String text) {
		try {
			String convert = "";
			for (int j = 0; j < text.length(); j++) {
				char word = text.charAt(j);
				String[] pinyinArray = null;
				if (java.lang.Character.toString(word).matches("[\\u4E00-\\u9FA5]+")) {
					pinyinArray = PinyinHelper.toHanyuPinyinStringArray(word, new HanyuPinyinOutputFormat());
				}
				if (pinyinArray == null) {
					convert += word;
				} else {
					convert += pinyinArray[0].charAt(0);
				}
			}
			return convert;
		} catch (Exception e) {
			logger.debug("转换拼音首字母出错,返回原字符", e);
			return text;
		}
	}

	/**
	 * 获取ascii码
	 * 
	 * @param text
	 *            原字符
	 * @author 张军
	 * @date 2016-2-1 9:16:00
	 * @modifiyNote
	 * @version 1.0
	 * @return ascii码
	 */
	public static String getCnASCII(String cnStr) {
		StringBuffer strBuf = new StringBuffer();
		byte[] bGBK = cnStr.getBytes();
		for (int i = 0; i < bGBK.length; i++) {
			// System.out.println(Integer.toHexString(bGBK[i]&0xff));
			strBuf.append(Integer.toHexString(bGBK[i] & 0xff));
		}
		return strBuf.toString();
	}
}

pinyin4j-2.5.1.zip



更多文章、技术交流、商务合作、联系博主

微信扫码或搜索:z360901061

微信扫一扫加我为好友

QQ号联系: 360901061

您的支持是博主写作最大的动力,如果您喜欢我的文章,感觉我的文章对您有帮助,请用微信扫描下面二维码支持博主2元、5元、10元、20元等您想捐的金额吧,狠狠点击下面给点支持吧,站长非常感激您!手机微信长按不能支付解决办法:请将微信支付二维码保存到相册,切换到微信,然后点击微信右上角扫一扫功能,选择支付二维码完成支付。

【本文对您有帮助就好】

您的支持是博主写作最大的动力,如果您喜欢我的文章,感觉我的文章对您有帮助,请用微信扫描上面二维码支持博主2元、5元、10元、自定义金额等您想捐的金额吧,站长会非常 感谢您的哦!!!

发表我的评论
最新评论 总共0条评论