转载请注明出处:http://blog.csdn.net/l1028386804/article/details/55505397

pinyin4J 是一个可以将汉字转换成拼音的lib,非常实用,其maven地址为:http://mvnrepository.com/artifact/com.belerweb/pinyin4j/2.5.0
pinyin4J 提供PinyinHelper这个静态类对外提供拼音转换的服务,主要有一下方法:

static public String[] toHanyuPinyinStringArray(char ch)

将char(必须为汉字单字)转化为拼音,实用的是通用的格式,如果ch为非汉字,返回null。
输入:重 输出:[zhong4, chong2]
说明重字有两个读音,拼音后面的1,2,3,4 代表的是读音

static public String[] toHanyuPinyinStringArray(char ch,HanyuPinyinOutputFormat outputFormat)

同上,但是这个方法可以设置输出的格式。HanyuPinyinOutputFormat   可以设置拼音大小写、是否后面加读音数字、特殊读音的显示方式,定义如下:

/*** The option indicates that the output of 'ü' is "u:"*/
public static final HanyuPinyinVCharType WITH_U_AND_COLON = new HanyuPinyinVCharType("WITH_U_AND_COLON");
/*** The option indicates that the output of 'ü' is "v"*/
public static final HanyuPinyinVCharType WITH_V = new HanyuPinyinVCharType("WITH_V")
/*** The option indicates that the output of 'ü' is "ü" in Unicode form*/
public static final HanyuPinyinVCharType WITH_U_UNICODE = new HanyuPinyinVCharType("WITH_U_UNICODE");
static public String[] toTongyongPinyinStringArray(char ch)  //转换为通用拼音
static public String[] toWadeGilesPinyinStringArray(char ch)    //转换为威妥玛拼音
static public String[] toMPS2PinyinStringArray(char ch)         //转换为注音符号拼音
static public String[] toYalePinyinStringArray(char ch)         //转换为耶魯拼音
static public String[] toGwoyeuRomatzyhStringArray(char ch)     //转换为国语罗马字

对于”重“的拼音转换,以上方法分别得到的结果是:

汉语拼音:[zhong4, chong2]
通用拼音:[jhong4, chong2]
威妥玛拼音:[chung4, ch`ung2]
注音符号拼音:[jung4, chung2]
耶魯拼音:[jung4, chung2]
国语罗马字:[jonq, chorng]

好了,有了上面的基础,我们可以封装一个工具类,用来将汉字转换成拼音,这里只使用了汉字拼音。
首先要将pinyin4j加入项目中,如果是maven项目,可以添加引用:

<dependency><groupId>com.belerweb</groupId><artifactId>pinyin4j</artifactId><version>2.5.0</version>
</dependency>

非maven的可以直接将下载好的jar包放入classpath。
然后编写工具类 PinyinTool.java:

package com.lyz.pingyin;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 liuyazhuang**/
public class PinyinTool {HanyuPinyinOutputFormat format = null;public static enum Type {UPPERCASE,              //全部大写LOWERCASE,              //全部小写FIRSTUPPER              //首字母大写}public PinyinTool(){format = new HanyuPinyinOutputFormat();format.setCaseType(HanyuPinyinCaseType.UPPERCASE);format.setToneType(HanyuPinyinToneType.WITHOUT_TONE);}public String toPinYin(String str) throws BadHanyuPinyinOutputFormatCombination{return toPinYin(str, "", Type.UPPERCASE);}public String toPinYin(String str,String spera) throws BadHanyuPinyinOutputFormatCombination{return toPinYin(str, spera, Type.UPPERCASE);}/*** 将str转换成拼音,如果不是汉字或者没有对应的拼音,则不作转换* 如: 明天 转换成 MINGTIAN* @param str:要转化的汉字* @param spera:转化结果的分割符* @return* @throws BadHanyuPinyinOutputFormatCombination*/public String toPinYin(String str, String spera, Type type) throws BadHanyuPinyinOutputFormatCombination {if(str == null || str.trim().length()==0)return "";if(type == Type.UPPERCASE)format.setCaseType(HanyuPinyinCaseType.UPPERCASE);elseformat.setCaseType(HanyuPinyinCaseType.LOWERCASE);String py = "";String temp = "";String[] t;for(int i=0;i<str.length();i++){char c = str.charAt(i);if((int)c <= 128)py += c;else{t = PinyinHelper.toHanyuPinyinStringArray(c, format);if(t == null)py += c;else{temp = t[0];if(type == Type.FIRSTUPPER)temp = t[0].toUpperCase().charAt(0)+temp.substring(1);py += temp+(i==str.length()-1?"":spera);}}}return py.trim();}
}

编写测试类PingyinToolTest

package com.lyz.pingyin.test;import com.lyz.pingyin.PinyinTool;
import com.lyz.pingyin.PinyinTool.Type;/*** 测试拼音转化结果* @author liuyazhuang**/
public class PingyinToolTest {public static void main(String[] args) throws Exception{PinyinTool tool = new PinyinTool();System.out.println("刘亚壮的运行测试结果为====" + tool.toPinYin("刘亚壮", "", Type.LOWERCASE));}
}

运行测试结果如下:

刘亚壮的运行测试结果为====liuyazhuang

Java之——汉字转换拼音(大小写)相关推荐

  1. java 实现汉字转换拼音_JAVA实现汉字转拼音功能代码实例

    JAVA中汉字转拼音的方法并不复杂,可以使用pinyin4j包来实现. 一.下载pinyin4j的架包,并导入项目中,如下: 如果是maven项目,maven依赖如下: com.belerweb pi ...

  2. java 实现汉字转换拼音_Java实现汉字转换为拼音

    # re: Java实现汉字转换为拼音 2006-11-24 15:06 芦苇 JAVA将汉字转化成拼音的方法 /** *//** ################################## ...

  3. java版汉字转换拼音(大小写)

    Java面试笔试面经.Java技术每天学习一点 公众号Java面试 关注我不迷路 作者:集成显卡 来源:https://blog.csdn.net/ssrc0604hx/ pinyin4J 是一个可以 ...

  4. [pinyin4j] java版汉字转换拼音(大小写)

    pinyin4J 是一个可以将汉字转换成拼音的lib,非常实用,其maven地址为:http://mvnrepository.com/artifact/com.belerweb/pinyin4j/2. ...

  5. Java实现汉字转换拼音功能

    使用工具类: <dependency><groupId>com.belerweb</groupId><artifactId>pinyin4j</a ...

  6. 【你不知道的Java】-汉字转换拼音

    一: String cnStr = "钓鱼岛是中国的"; diao yu dao shi zhong guo de 二:code public static String getP ...

  7. java汉字转换拼音

    1.汉字转换拼音首先引入一个叫pinyin4j-2.5.0.jar 下载地址:http://download.csdn.net/detail/yao__shun__yu/4670228 2.测试代码 ...

  8. java 汉字转换拼音

    java 汉字转换拼音 maven依赖 <dependency><groupId>com.belerweb</groupId><artifactId>p ...

  9. Java汉字转换拼音工具类

    1. 使用pinyin4j 1.1 引入相关maven依赖 <dependency><groupId>com.belerweb</groupId><artif ...

最新文章

  1. 浏览新闻oracle的数据结构,oracle 数据结构探索之旅二 [2]
  2. postmethod 设置request body utf-8_Cypress系列(62) request() 命令详解
  3. 解决阿里云 ssh 远程连接短时间没操作就会断掉的问题
  4. 0xFFFFFF的问题
  5. duration java_Java Duration类| ofMinutes()方法与示例
  6. Intel Core Solo/Duo处理器架构/微架构/流水线 - 前端/数据预取/SSE3
  7. bootstrap 输入错误提示_网上体育用品商城(ssm,mysql,bootstrap,html,css)
  8. 网站SEO优化之Robots.txt文件写法。
  9. 【OS笔记 4】操作系统的组织结构(层次结构、微内核结构)虚拟机的概念
  10. 云计算认证哪个好?考什么内容?
  11. java home websphere_websphere6.1安装与配置
  12. 【云栖大会】2016 杭州云栖大会随笔
  13. 大学计算机案例教程旧照片修复,破损旧照片修复教程
  14. chrome 浏览器开发者工具之网络面板
  15. oracle索引介绍
  16. [Centos7]Mirai QQ机器人监听播报TeamSpeak3用户状态
  17. jira linux 一键安装包下载,JIRA使用教程:使用文件包安装JIRA
  18. 每日新闻:百度总裁张亚勤:开放合作是AI时代全球大势;青云QingCloud与思杰达成战略合作...
  19. 粉末冶金、功能陶瓷等新材料的高温热成型设备
  20. 【干货】在PPT如何快速添加时间轴

热门文章

  1. 《出入库管理系统》软件主页与FAQ
  2. MNIST手写数字识别 —— 图像分析法实现二分类
  3. Keras Sequential顺序模型
  4. 某985大学 软件工程专硕,复试线暴涨60分!
  5. 016-OpenCV 图像对比度亮度调整
  6. 页面加载前需要定义全局变量
  7. 提高表达力的几个方法
  8. 已解决Makefile:162:recipe for target ‘all‘ failed
  9. 2020电力英语及计算机考试题库,电力英语考试题库
  10. Ubuntu16.04比较好的一系列软件安装介绍