java实现将汉字转为拼音并包含音调
有时候为了方便操作程序的开发,需要将汉字转为拼音等操作。下面这个是自己结合网上的资料,加上自己在公司项目中的亲自实践。完整的实现了将汉字转为拼音的操作。这个Demo只是负责将其转换,在main方法中测试,在实际需要中,只需要调用这个类中的方法即可。
首先贴出测试结果:
测试参数:
汉字转换为拼音
汉字转换为拼音
main测试方法的代码:
public static void main(String[] args) {System.out.println(ToFirstChar("汉字转换为拼音").toUpperCase()); //转为首字母大写System.out.println(ToPinyin("汉字转换为拼音"));
}
本功能的实现时利用java开源库,开发此程序需要一个jar包。本人用的是pinyin.jar。网上可以直接下载,也可以在其官网进行下载。在此不祥述。如果实在不乐意,可以点击下载将进行这个jar包的下载。
贴出实现该Demo的源码:
package com.red.test;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.exception.BadHanyuPinyinOutputFormatCombination;/*** 汉字转换为拼音* @author Red*/
public class PinyinDemo {/*** 测试main方法* @param args*/public static void main(String[] args) {System.out.println(ToFirstChar("汉字转换为拼音").toUpperCase()); //转为首字母大写System.out.println(ToPinyin("汉字转换为拼音")); }/*** 获取字符串拼音的第一个字母* @param chinese* @return*/public static String ToFirstChar(String chinese){ String pinyinStr = ""; char[] newChar = chinese.toCharArray(); //转为单个字符HanyuPinyinOutputFormat defaultFormat = new HanyuPinyinOutputFormat(); defaultFormat.setCaseType(HanyuPinyinCaseType.LOWERCASE); defaultFormat.setToneType(HanyuPinyinToneType.WITHOUT_TONE); for (int i = 0; i < newChar.length; i++) { if (newChar[i] > 128) { try { pinyinStr += PinyinHelper.toHanyuPinyinStringArray(newChar[i], defaultFormat)[0].charAt(0); } catch (BadHanyuPinyinOutputFormatCombination e) { e.printStackTrace(); } }else{ pinyinStr += newChar[i]; } } return pinyinStr; } /*** 汉字转为拼音* @param chinese* @return*/public static String ToPinyin(String chinese){ String pinyinStr = ""; char[] newChar = chinese.toCharArray(); HanyuPinyinOutputFormat defaultFormat = new HanyuPinyinOutputFormat(); defaultFormat.setCaseType(HanyuPinyinCaseType.LOWERCASE); defaultFormat.setToneType(HanyuPinyinToneType.WITHOUT_TONE); for (int i = 0; i < newChar.length; i++) { if (newChar[i] > 128) { try { pinyinStr += PinyinHelper.toHanyuPinyinStringArray(newChar[i], defaultFormat)[0]; } catch (BadHanyuPinyinOutputFormatCombination e) { e.printStackTrace(); } }else{ pinyinStr += newChar[i]; } } return pinyinStr; }
}
补充:查询拼音全拼并包含音调
public static String getPinYin(String src) {char[] t1 = null;t1 = src.toCharArray();String[] t2 = new String[t1.length];//t3是全部的拼音,不带声调HanyuPinyinOutputFormat t3 = new HanyuPinyinOutputFormat();t3.setCaseType(HanyuPinyinCaseType.LOWERCASE);t3.setToneType(HanyuPinyinToneType.WITHOUT_TONE);t3.setVCharType(HanyuPinyinVCharType.WITH_V);//format是全部的拼音并且带声调HanyuPinyinOutputFormat format= new HanyuPinyinOutputFormat();format.setToneType(HanyuPinyinToneType.WITH_TONE_MARK);format.setVCharType(HanyuPinyinVCharType.WITH_U_UNICODE);String t4 = "";int t0 = t1.length;try {for (int i = 0; i < t0; i++) {// 判断是否为汉字字符if (java.lang.Character.toString(t1[i]).matches("[\\u4E00-\\u9FA5]+")) {
// t2 = PinyinHelper.toHanyuPinyinStringArray(t1[i], t3);t2 = PinyinHelper.toHanyuPinyinStringArray(t1[i], format);t4 += t2[0]+" ";} else {t4 += java.lang.Character.toString(t1[i]);}}return t4;} catch (BadHanyuPinyinOutputFormatCombination e1) {e1.printStackTrace();}return t4;}
java实现将汉字转为拼音并包含音调相关推荐
- java实现将汉字转为拼音
原文:java实现将汉字转为拼音 有时候为了方便操作程序的开发,需要将汉字转为拼音等操作.下面这个是自己结合网上的资料,加上自己在公司项目中的亲自实践.完整的实现了将汉字转为拼音的操作.这个Demo只 ...
- java实现汉字转为拼音
java实现汉字转为拼音: 1.需要导入pinyin4j.jar package com.loo.pinyin; import net.sourceforge.pinyin4j.PinyinHelpe ...
- Java实用工具类-将汉字转为拼音
1. 导入对应的jar包 <dependency><groupId>com.belerweb</groupId><artifactId>pinyin4j ...
- Java汉字转为拼音工具类
依赖文件 <!-- https://mvnrepository.com/artifact/com.belerweb/pinyin4j --><dependency><gr ...
- 【实例】PHP如何实现汉字转为拼音的?
php汉字转拼音本次使用的是 overtrue/pinyin 扩展.基于 CC-CEDICT 词典的中文转拼音工具,更准确的支持多音字的汉字转拼音解决方案. 一.安装 1.使用 Composer 安装 ...
- android中汉字转为拼音
汉语转为拼音 首先要先导入架包:com.belerweb:pinyin4j:2.5.1 工具类: 注释的方法,是可以将单个汉字转为拼音的,为注释的可以转化词语, public class HanZiT ...
- python第三方库:pypinyin将汉字转为拼音
汉字的拼音虽然有一定的规律,但是做一套好的汉字转拼音的系统并不是那么容易,需要考虑的问题也比较多.汉字转拼音在多个的方向上也经常使用到.比如在url中,很少使用中文作为url连接,一种方式是转换为拼音 ...
- Java将汉字转为拼音
汉字转换成拼音 : 首先jar:pinyin4j-2.5.0.jar 或: <dependency><groupId>com.belerweb</groupId>& ...
- Java中将汉字转为拼音
转载请标明出处:http://blog.csdn.net/liu1252247624/article/details/51553825 源码地址:http://download.csdn.net/de ...
- java hanyupinyinoutputformat();_Java汉字转拼音pinyin4j用法
Java汉字转拼音pinyin4j用法 在学习编程的过程中,我觉得不止要获得课本的知识, 更多的是通过学习技术知识提高解决问题的能力,这样我们才能走在最前方,更多Java学习,请登陆疯狂java官网. ...
最新文章
- 比利时皇家科学院院士Luc De Raedt:从统计关系人工智能到神经符号计算
- Java连接Mysql数据库增删改查实现
- Keil uVision4 for ARM 下增加支持C51,C5x
- python优先级排序_python中使用优先队列
- 一种极端思维引起的幻觉
- 黑域助手连接服务器才能用吗,黑域app怎么使用?进入黑域详细教程[图]
- c语言中十六进制可以直接和十进制运算吗,C语言 · 十六进制转十进制
- MT【99】2005联赛二试题我的一行解法
- 洛谷P1195 口袋的天空
- 单元测试中 Right-BICEP 和 CORRECT
- 清华同方锋锐 u430 更换内存条教程
- plsql 客户端字符集_plsql查看数据库字符集
- 高新技术企业的申请条件
- Windows 7登陆时自动创建TEMP临时文件夹问题解决
- linux软件安装 home,Ubuntu中安装建筑设计软件Sweet Home 3D
- 计算机休眠策略,桌面从睡眠或休眠中意外唤醒
- 微信小程序实现pdf、word等格式文件上传的方法,微信小程序word文档
- 查看实时网速的小方法
- PAT乙级(Basic Level)练习题 客似云来
- 超实用的浏览器插件:CSDN全站去广告