pinyin4j是一个功能强悍的汉语拼音工具包,主要是从汉语获取各种格式和需求的拼音,功能强悍,下面看看如何使用pinyin4j。
下面介绍用pinyin4j来做的一个根据汉语获取各种格式和需求的拼音demo
需要pinyin4j.jar自己去官网下载去吧http://pinyin4j.sourceforge.net/

  1. import java.util.HashSet;
  2. import java.util.Set;
  3. import net.sourceforge.pinyin4j.PinyinHelper;
  4. import net.sourceforge.pinyin4j.format.HanyuPinyinCaseType;
  5. import net.sourceforge.pinyin4j.format.HanyuPinyinOutputFormat;
  6. import net.sourceforge.pinyin4j.format.HanyuPinyinToneType;
  7. import net.sourceforge.pinyin4j.format.HanyuPinyinVCharType;
  8. import net.sourceforge.pinyin4j.format.exception.BadHanyuPinyinOutputFormatCombination;
  9. /**
  10. *
  11. * @author  : Robert robert@xiaobei668.com
  12. * @version : 1.00
  13. * Create Time : 2011-3-22-下午07:04:30
  14. * Description :
  15. *              处理汉字和对应拼音转换的工具类
  16. * History:
  17. *  Editor       version      Time               Operation    Description*
  18. *
  19. *
  20. */
  21. public class PinYinUtil {
  22. /**
  23. *
  24. * @param src
  25. * @return
  26. * author  : Robert
  27. * about version :1.00
  28. * create time   : 2011-3-22-下午07:04:27
  29. * Description :
  30. *             传入汉字字符串,拼接成对应的拼音,返回拼音的集合
  31. */
  32. public static Set<String> getPinYinSet(String src){
  33. Set<String> lstResult = new HashSet<String>();
  34. char[] t1 = null;  //字符串转换成char数组
  35. t1 = src.toCharArray();
  36. //①迭代汉字
  37. for(char ch : t1){
  38. String s[] = getPinYin(ch);
  39. Set<String> lstNew = new HashSet<String>();
  40. //②迭代每个汉字的拼音数组
  41. for(String str : s){
  42. if(lstResult.size()==0){
  43. lstNew.add(str);
  44. }else{
  45. for(String ss : lstResult){
  46. ss += str;
  47. lstNew.add(ss);
  48. }
  49. }
  50. }
  51. lstResult.clear();
  52. lstResult = lstNew;
  53. }
  54. return lstResult;
  55. }
  56. public static void main(String[] args) {
  57. Set<String> lst = PinYinUtil.getPinYinSet("迭代每个汉字的拼音数组,该分享来自程序员之家");
  58. for (String string : lst) {
  59. System.out.println(string);
  60. }
  61. }
  62. /**
  63. *
  64. * @param src
  65. * @return
  66. * author  : Robert
  67. * about version :1.00
  68. * create time   : 2011-3-22-下午02:21:42
  69. * Description :
  70. *             传入中文汉字,转换出对应拼音
  71. *             注:出现同音字,默认选择汉字全拼的第一种读音
  72. */
  73. public static String getPinYin(String src) {
  74. char[] t1 = null;
  75. t1 = src.toCharArray();
  76. String[] t2 = new String[t1.length];
  77. // 设置汉字拼音输出的格式
  78. HanyuPinyinOutputFormat t3 = new HanyuPinyinOutputFormat();
  79. t3.setCaseType(HanyuPinyinCaseType.LOWERCASE);
  80. t3.setToneType(HanyuPinyinToneType.WITHOUT_TONE);
  81. t3.setVCharType(HanyuPinyinVCharType.WITH_V);
  82. String t4 = "";
  83. int t0 = t1.length;
  84. try {
  85. for (int i = 0; i < t0; i++) {
  86. // 判断能否为汉字字符
  87. // System.out.println(t1[i]);
  88. if (Character.toString(t1[i]).matches("[\\u4E00-\\u9FA5]+")) {
  89. t2 = PinyinHelper.toHanyuPinyinStringArray(t1[i], t3);// 将汉字的几种全拼都存到t2数组中
  90. t4 += t2[0];// 取出该汉字全拼的第一种读音并连接到字符串t4后
  91. } else {
  92. // 如果不是汉字字符,间接取出字符并连接到字符串t4后
  93. t4 += Character.toString(t1[i]);
  94. }
  95. }
  96. } catch (BadHanyuPinyinOutputFormatCombination e) {
  97. e.printStackTrace();
  98. }
  99. return t4;
  100. }
  101. /**
  102. * @param src
  103. * @return
  104. * author  : Robert
  105. * about version :1.00
  106. * create time   : 2011-3-22-下午02:52:35
  107. * Description :
  108. *             将单个汉字转换成汉语拼音,考虑到同音字问题,返回字符串数组的形式
  109. */
  110. public static String[] getPinYin(char src){
  111. char[] t1 = {src};
  112. String[] t2 = new String[t1.length];
  113. // 设置汉字拼音输出的格式
  114. HanyuPinyinOutputFormat t3 = new HanyuPinyinOutputFormat();
  115. t3.setCaseType(HanyuPinyinCaseType.LOWERCASE);
  116. t3.setToneType(HanyuPinyinToneType.WITHOUT_TONE);
  117. t3.setVCharType(HanyuPinyinVCharType.WITH_V);
  118. // 判断能否为汉字字符
  119. if (Character.toString(t1[0]).matches("[\\u4E00-\\u9FA5]+")) {
  120. try {
  121. // 将汉字的几种全拼都存到t2数组中
  122. t2 = PinyinHelper.toHanyuPinyinStringArray(t1[0], t3);
  123. } catch (BadHanyuPinyinOutputFormatCombination e) {
  124. e.printStackTrace();
  125. }
  126. } else {
  127. // 如果不是汉字字符,则把字符直接放入t2数组中
  128. t2[0] = String.valueOf(src);
  129. }
  130. return t2;
  131. }
  132. /**
  133. *
  134. * @param src
  135. * @return
  136. * author  : Robert
  137. * about version :1.00
  138. * create time   : 2011-3-22-下午03:03:02
  139. * Description :
  140. *             传入没有多音字的中文汉字,转换出对应拼音
  141. *             注:如果传入的中文中有任一同音字都会返回字符串信息:false
  142. */
  143. public static String getNoPolyphone(String src){
  144. char[] t1 = null;
  145. t1 = src.toCharArray();
  146. String[] t2 = new String[t1.length];
  147. // 设置汉字拼音输出的格式
  148. HanyuPinyinOutputFormat t3 = new HanyuPinyinOutputFormat();
  149. t3.setCaseType(HanyuPinyinCaseType.LOWERCASE);
  150. t3.setToneType(HanyuPinyinToneType.WITHOUT_TONE);
  151. t3.setVCharType(HanyuPinyinVCharType.WITH_V);
  152. String t4 = "";
  153. int t0 = t1.length;
  154. try {
  155. for (int i = 0; i < t0; i++) {
  156. // 判断能否为汉字字符
  157. // System.out.println(t1[i]);
  158. if (Character.toString(t1[i]).matches("[\\u4E00-\\u9FA5]+")) {
  159. t2 = PinyinHelper.toHanyuPinyinStringArray(t1[i], t3);// 将汉字的几种全拼都存到t2数组中
  160. if(t2.length>1){
  161. return "false";
  162. }else{
  163. t4 += t2[0];// 取出该汉字全拼的第一种读音并连接到字符串t4后
  164. }
  165. } else {
  166. // 如果不是汉字字符,间接取出字符并连接到字符串t4后
  167. t4 += Character.toString(t1[i]);
  168. }
  169. }
  170. } catch (BadHanyuPinyinOutputFormatCombination e) {
  171. e.printStackTrace();
  172. }
  173. return t4;
  174. }
  175. }

复制代码

推荐文章:Java根据汉字字符串检索出字符首字母

转载自:http://bbs.it-home.org/thread-3093-1-1.html

[Java代码] Java用pinyin4j根据汉语获取各种格式和需求的拼音的更多相关文章

  1. java 代码实现使用Druid 链接池获取数据库链接

    因为原先使用的c3p0链接池,时常出现:APPARENT DEADLOCK!!! Creating emergency threads for unassigned pending tasks,以及出 ...

  2. [改善Java代码]Java的泛型是类型擦除的

    泛型可以减少强制类型的转换,可规范集合的元素类型,还可以提高代码的安全性和可读性,正是因为有了这些优点,自从Java引入泛型之后,项目的编码规则上便多了一条,优先使用泛型. Java泛型(Generi ...

  3. [Java代码] Java是自学好还是参加培训班好?

    ava 是由Sun Microsystems公司于1995年5月推出的高级程序设计语言. Java可运行于多个平台,如Windows, Mac OS,及其他多种UNIX版本的系统. 本教程给大家简单介 ...

  4. java代码-----------java中的windowAdapter的实例

    总结:我知道他是一专多能型.很优秀~~~~~~~~~~~.好幸福啊 package com.a.b; import java.awt.Color; import java.awt.event.Wind ...

  5. python调用java代码 java虚拟机(jvm)

    1.新建com文件夹,在里面新建 fibnq.java package com; public class fibnq { public fibnq(){} public int fb(int n){ ...

  6. wsdl 生成 java 代码 java 使用CXF将wsdl文件生成客户端代码命令java调用第三方的webservice应用实例 推荐使用, 并且设置了 utf8

    推荐使用, 并且设置了 utf8 wsdl2java -p cn.smborderservice  -encoding utf-8 -d f:\logink\src -all -autoNameRes ...

  7. JAVA代码实现嵌套层级列表,POI导出嵌套层级列表

    要实现POI导出EXCEL形如 --A1(LV1) ----B1(LV2) ----B2(LV2) ------C1(LV3) ------C2(LV3) ----B3(LV2) --A1(LV1)

  8. 使用非java代码编程

    使用非JAVA代码     JAVA语言及其标准API(应用程序编程接口)应付应用程序的编写已绰绰有余.但在某些情况下,还是必须使用非JAVA编码.例如,我们有时要访问操作系统的专用特性,与特殊的硬件 ...

  9. Java代码更改shape和selector文件的颜色值

    Android里面经常会使用shape或者selector来定制一些View的背景.那么如果想要动态更改shape或者seletor文件中的颜色值,该怎么处理呢? 一.Java代码更改shape的颜色 ...

随机推荐

  1. 关于用舞蹈链DLX算法求解数独的解析

    欢迎访问——该文出处-博客园-zhouzhendong 去博客园看该文章--传送门 描述 在做DLX算法题中,经常会做到数独类型的题目,那么,如何求解数独类型的题目?其实,学了数独的构建方法,那么DL ...

  2. kmp基础 ekmp

    +]; int lenp,lens; +];//可以是char 也可以是string +]; void getnext() { nex[]=-; ,j=; ) { ||p[j]==p[k]) nex[ ...

  3. 数据特征分析:1.基础分析概述& 分布分析

    基础分析概述 几个基础分析思路: 分布分析 对比分析 统计分析 帕累托分析 正态性检测 相关性分析 分布分析 分布分析是研究数据的分布特征和分布类型,分定量数据.定性数据区分基本统计量. import ...

  4. JavaSE| 流程控制

    程序流程控制 流程控制语句结构: .顺序结构 语句的关系是从上到下依次执行的顺序关系,中间没有任何判断和跳转: 它是最基本的结构,Java程序总体来说都是从main()依次执行 .分支结构: 根据条件 ...

  5. 007 numpy数组文件的存取

    不知道这个有没有用,都整理了一番. 一:数组以二进制格式进行存储 1.说明 np.save与np.load是读写磁盘数组数据的两个重要函数. 默认情况下,数组以压缩的原始二进制格式保存在扩展名为npy ...

  6. day 49-css补充(终结)[浮动和定位]

    老师的笔记: 前情回顾: day49 混乱即阶梯. 1. 前情回顾 HTML HTTP和HTML 文档机构 <!Doctype html> HTML head <meta> & ...

  7. OpenCV-Python 中文教程(搬运)目录

    OpenCV-Python 中文教程 OpenCV官方教程中文版(For Python) OpenCV2-Python-Tutorials 段力辉 译 说明:搬运自linux公社pdf文件,粗略搬运, ...

  8. 安卓编程资源文件string中对占位符的使用详解

    这里将为你详细介绍占位符的使用,将其学以致用,可以达到简化布局文件,减少字符串资源量. 1.在资源文件中的使用. 打开资源文件中的strings.xml文件,进行编辑.如下图所示: 图  1.0 2. ...

  9. Looping through the content of a file in Bash

    https://stackoverflow.com/questions/1521462/looping-through-the-content-of-a-file-in-bash One way to ...

  10. SpringBoot+Jpa+MySql学习

    上一篇介绍了springboot简单整合mybatis的教程.这一篇是介绍springboot简单整合jpa的教程. 由于jpa的功能强大,后续会继续写关于jpa的介绍已经使用,本文只是简单介绍一下它 ...