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   可以设置拼音大小写、是否后面加读音数字、特殊读音的显示方式,定义如下:

[java] view plain copy
  1. /**
  2. * The option indicates that the output of 'ü' is "u:"
  3. */
  4. public static final HanyuPinyinVCharType WITH_U_AND_COLON = new HanyuPinyinVCharType("WITH_U_AND_COLON");
  5. /**
  6. * The option indicates that the output of 'ü' is "v"
  7. */
  8. public static final HanyuPinyinVCharType WITH_V = new HanyuPinyinVCharType("WITH_V")
  9. /**
  10. * The option indicates that the output of 'ü' is "ü" in Unicode form
  11. */
  12. public static final HanyuPinyinVCharType WITH_U_UNICODE = new HanyuPinyinVCharType("WITH_U_UNICODE");

static public String[] toTongyongPinyinStringArray(char ch)

转换为通用拼音。通用拼音的介绍见:http://zh.wikipedia.org/zh-cn/%E9%80%9A%E7%94%A8%E6%8B%BC%E9%9F%B3

static public String[] toWadeGilesPinyinStringArray(char ch)

转换为威妥玛拼音:http://zh.wikipedia.org/wiki/%E5%A8%81%E5%A6%A5%E7%91%AA%E6%8B%BC%E9%9F%B3

static public String[] toMPS2PinyinStringArray(char ch)

转换为注音符号拼音:http://zh.wikipedia.org/zh-cn/%E6%B3%A8%E9%9F%B3%E7%AC%A6%E8%99%9F

static public String[] toYalePinyinStringArray(char ch)

转换为耶魯拼音:http://zh.wikipedia.org/zh-cn/%E8%80%B6%E9%AD%AF%E6%8B%BC%E9%9F%B3

static public String[] toGwoyeuRomatzyhStringArray(char ch)

转换为国语罗马字:http://zh.wikipedia.org/wiki/%E5%9C%8B%E8%AA%9E%E7%BE%85%E9%A6%AC%E5%AD%97

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

[java] view plain copy
  1. 汉语拼音:[zhong4, chong2]
  2. 通用拼音:[jhong4, chong2]
  3. 威妥玛拼音:[chung4, ch`ung2]
  4. 注音符号拼音:[jung4, chung2]
  5. 耶魯拼音:[jung4, chung2]
  6. 国语罗马字:[jonq, chorng]

好了,有了上面的基础,我们可以封装一个工具类,用来将汉字转换成拼音,这里只使用了汉字拼音。

首先要将pinyin4j加入项目中,如果是maven项目,可以添加引用:

[java] view plain copy
  1. <span style="white-space:pre">    </span><!-- 增加pinyin4j -->
  2. <dependency>
  3. <groupId>com.belerweb</groupId>
  4. <artifactId>pinyin4j</artifactId>
  5. <version>2.5.0</version>
  6. </dependency>

非maven的可以直接将下载好的jar包放入classpath。

然后编写工具类 PinyinTool.java:

[java] view plain copy
  1. package org.nerve.d3lesson.common.tools;
  2. import net.sourceforge.pinyin4j.PinyinHelper;
  3. import net.sourceforge.pinyin4j.format.HanyuPinyinCaseType;
  4. import net.sourceforge.pinyin4j.format.HanyuPinyinOutputFormat;
  5. import net.sourceforge.pinyin4j.format.HanyuPinyinToneType;
  6. import net.sourceforge.pinyin4j.format.exception.BadHanyuPinyinOutputFormatCombination;
  7. import java.util.Arrays;
  8. /**
  9. *
  10. * Created by zengxm on 2014/12/4.
  11. */
  12. public class PinyinTool {
  13. HanyuPinyinOutputFormat format = null;
  14. public static enum Type {
  15. UPPERCASE,              //全部大写
  16. LOWERCASE,              //全部小写
  17. FIRSTUPPER              //首字母大写
  18. }
  19. public PinyinTool(){
  20. format = new HanyuPinyinOutputFormat();
  21. format.setCaseType(HanyuPinyinCaseType.UPPERCASE);
  22. format.setToneType(HanyuPinyinToneType.WITHOUT_TONE);
  23. }
  24. public String toPinYin(String str) throws BadHanyuPinyinOutputFormatCombination{
  25. return toPinYin(str, "", Type.UPPERCASE);
  26. }
  27. public String toPinYin(String str,String spera) throws BadHanyuPinyinOutputFormatCombination{
  28. return toPinYin(str, spera, Type.UPPERCASE);
  29. }
  30. /**
  31. * 将str转换成拼音,如果不是汉字或者没有对应的拼音,则不作转换
  32. * 如: 明天 转换成 MINGTIAN
  33. * @param str
  34. * @param spera
  35. * @return
  36. * @throws BadHanyuPinyinOutputFormatCombination
  37. */
  38. public String toPinYin(String str, String spera, Type type) throws BadHanyuPinyinOutputFormatCombination {
  39. if(str == null || str.trim().length()==0)
  40. return "";
  41. if(type == Type.UPPERCASE)
  42. format.setCaseType(HanyuPinyinCaseType.UPPERCASE);
  43. else
  44. format.setCaseType(HanyuPinyinCaseType.LOWERCASE);
  45. String py = "";
  46. String temp = "";
  47. String[] t;
  48. for(int i=0;i<str.length();i++){
  49. char c = str.charAt(i);
  50. if((int)c <= 128)
  51. py += c;
  52. else{
  53. t = PinyinHelper.toHanyuPinyinStringArray(c, format);
  54. if(t == null)
  55. py += c;
  56. else{
  57. temp = t[0];
  58. if(type == Type.FIRSTUPPER)
  59. temp = t[0].toUpperCase().charAt(0)+temp.substring(1);
  60. py += temp+(i==str.length()-1?"":spera);
  61. }
  62. }
  63. }
  64. return py.trim();
  65. }
  66. }

写个测试用例看看结果:

微信公众号:

转载于:https://www.cnblogs.com/Amos-Turing/p/6149010.html

[pinyin4j] java版汉字转换拼音(大小写)相关推荐

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

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

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

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

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

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

  4. Java之——汉字转换拼音(大小写)

    转载请注明出处:http://blog.csdn.net/l1028386804/article/details/55505397 pinyin4J 是一个可以将汉字转换成拼音的lib,非常实用,其m ...

  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实现汉字转换为拼音 pinyin4j/JPinyin

    转载自  JAVA实现汉字转换为拼音 pinyin4j/JPinyin 在项目中经常会遇到需求用户输入汉字后转换为拼音的场景,比如说通讯录,就会要求按名字首字符发音排序,如果自己写实现这方面的功能是个 ...

  9. java 汉字转换拼音

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

最新文章

  1. 工作只用jquery,原生js知道就好了
  2. Greenplum 类型一致性使用规范 - 索引条件、JOIN的类型一致性限制
  3. 【刘晓燕语法长难句】 并列句
  4. CenOS 6.5 RPM 安装 elasticsearch 6.3.1
  5. “长按地址在浏览器中打开”的解决办法
  6. 好用的代码加密软件,编程,编译数据安全
  7. windows的终端命令
  8. Visio Viewer 打开vsd文件
  9. 论文阅读 | Combating Adversarial Misspellings with Robust Word Recognition
  10. 使用uiautomatorviewer报错Error obtaining UI hierarchy
  11. K210入门必看(MAIX DOCK)(一)
  12. php redis中文手册——《redis中文手册》 php版
  13. Java stream().sorted()实现排序(升序、降序、多字段排序)
  14. MKR基于知识图谱的推荐算法
  15. .py文件与.ipynb文件互相转换
  16. 单元测试、注解、枚举、反射(5)JavaSE
  17. 英语练习32 Poor Amy
  18. 【哲学问题】-《哲学家们都干了些什么?》
  19. 怎样有效地阅读一篇论文?
  20. 保险公司舆情工作实施方案

热门文章

  1. JavaScript高级程序设计学习笔记(三)
  2. Android项目总结之社会化分享
  3. shell系列9-awk
  4. vue watch的用法及新旧值一样问题解决
  5. 知乎搜索关键字爬取相关图片
  6. 南京Uber优步司机奖励政策(1月18日~1月24日)
  7. php之box/spout 导入导出功能
  8. scrapy抓取淘宝女郎 1
  9. 能详细讲一下关于 18 世纪哲学家大卫 • 休谟和恐怖悖论吗
  10. river歌曲表达的意思_lost rivers是什么意思什么梗 歌曲含义与背后故事揭秘