pinyin4j的简单使用(中文字符和拼音之间的转换)

 

	private HanyuPinyinOutputFormat mOutputFormat = null;
		mOutputFormat = new HanyuPinyinOutputFormat();
		mOutputFormat.setCaseType(HanyuPinyinCaseType.LOWERCASE);//设置大小写
		mOutputFormat.setToneType(HanyuPinyinToneType.WITHOUT_TONE);//设置音调格式
		

	public String parserWordsToPinyin(String words) {
		if (words == null || words.trim().length() == 0) {
			return "###";
		}
		String py = "", temp = "";
		String[] t = null;
		for (int i = 0; i < words.length(); i++) {
			char c = words.charAt(i);
			if (((int) c) <= 128) {    //ascii 码小于128的都是字符数字之类
				py += c;
			} else {
				try {
					t = PinyinHelper.toHanyuPinyinStringArray(c, mOutputFormat);
					if (t == null) {
						py += c;
					} else {
						temp = t[0];
//						py += temp + (i == words.length() -1 ? "" : "_");
						py += temp;
					}
				} catch (BadHanyuPinyinOutputFormatCombination e) {
					e.printStackTrace();
				}
			}
		}
		return py.trim();
	}

pinyin4j-2.5.0.jar 下载路径:

https://download.csdn.net/download/myjie0527/10648124

 

参考自:https://blog.csdn.net/peekabo_o/article/details/50888322

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

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值