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

  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

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

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

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

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

  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:

  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. }

写个测试用例看看结果:

[pinyin4j] java版汉字转换拼音(大小写)的更多相关文章

  1. JAVA实现汉字转换为拼音 pinyin4j/JPinyin

    在项目中经常会遇到需求用户输入汉字后转换为拼音的场景,比如说通讯录,就会要求按名字首字符发音排序,如果自己写实现这方面的功能是个很好大的工程,还好网上有公开的第三方jar支持转换,结合网上很多前辈的代 ...

  2. java实现汉字转为拼音

    java实现汉字转为拼音: 1.需要导入pinyin4j.jar package com.loo.pinyin; import net.sourceforge.pinyin4j.PinyinHelpe ...

  3. JAVA实现汉字转拼音

    两个工具包都可以实现:pinyin4j/JPinyin pinyin4j 第一个是使用pinyin4j的jar,此jar对多音字语句的处理不太理想 package edu.ws; import net ...

  4. C#汉字转换拼音技术详解

    C#汉字转换拼音技术详解(高性能) 下面将源代码贴出.public static class ChineseToPinYin           {               private sta ...

  5. 【干货】JS版汉字与拼音互转终极方案,附简单的JS拼音输入法

    前言 网上关于JS实现汉字和拼音互转的文章很多,但是比较杂乱,都是互相抄来抄去,而且有的不支持多音字,有的不支持声调,有的字典文件太大,还比如有时候我仅仅是需要获取汉字拼音首字母却要引入200kb的字 ...

  6. JS版汉字与拼音互转终极方案,附简单的JS拼音输入法

    原文:http://www.cnblogs.com/liuxianan/p/pinyinjs.html 前言 网上关于JS实现汉字和拼音互转的文章很多,但是比较杂乱,都是互相抄来抄去,而且有的不支持多 ...

  7. JS版汉字与拼音互转终极方案,附简单的JS拼音

    前言 网上关于JS实现汉字和拼音互转的文章很多,但是比较杂乱,都是互相抄来抄去,而且有的不支持多音字,有的不支持声调,有的字典文件太大,还比如有时候我仅仅是需要获取汉字拼音首字母却要引入200kb的字 ...

  8. C#汉字转换拼音技术详解(高性能)

    public static class ChineseToPinYin { private static readonly Dictionary<<span class="key ...

  9. SQL中把汉字转换拼音码

    思路:在SQL中创建一个函数fn_GetPy(),函数的输入参数是一个汉字字符串,返回值是拼音码字符串. 创建函数语句: CREATE function fn_GetPy(@str nvarchar( ...

随机推荐

  1. SQL Server 远程链接服务器详细配置

    原文:SQL Server 远程链接服务器详细配置[转载] http://hi.baidu.com/luxi0194/blog/item/a5c2a9128a705cc6c2fd7803.html - ...

  2. 日期,为下拉列表添加日期,优化,目前本人博客上最优的解决方案,之前学习的通过判断得到平年闰年,而这个是让系统自动去判断,无须if判断,代码示例

    <%@ page language="java" import="java.util.*" pageEncoding="utf-8"% ...

  3. 大数据和Hadoop生态圈

    大数据和Hadoop生态圈 一.前言: 非常感谢Hadoop专业解决方案群:313702010,兄弟们的大力支持,在此说一声辛苦了,经过两周的努力,已经有啦初步的成果,目前第1章 大数据和Hadoop ...

  4. Dynamics CRM 2013 报表开发:安装开发工具

    最近项目需要开发报表,顺便看了下,首先需要配置开发环境.需要的工具为: 1.Business Intelligence Development Studio 可下载Sql Server 的安装包,选择 ...

  5. 取xml文件转成List<T>对象的两种方法

    读取xml文件转成List<T>对象的两种方法(附源码)   读取xml文件转成List<T>对象的两种方法(附源码) 读取xml文件,是项目中经常要用到的,所以就总结一下,最 ...

  6. 【IOS开发】《多线程编程指南》笔记

    线程是单个应用中可以并发执行多个代码路径的多种技术之一.虽然更新的技术如操作对象(Operation)和Grand Central Dispatch(GCD),提供一个等价现代化和高效的基础设施来实现 ...

  7. Oracle中复制表结构和表数据

    一.复制表结构及其数据 create table new_table as (select * from old_table); 二.只复制表结构 create table new_table as ...

  8. Aliexpress API 授权流程整理

    Aliexpress API 授权流程整理   前言 我零零总总用了好几个月的时间,写了一个自用的小程序,从 Aliexpress 上抓取订单的小程序.刚开始写的时候,该API还没有开放,而且没有订单 ...

  9. 启动mysql错误解决方案,学会查看错误日志:mysql.sock丢失,mysqld_safe启动报错

    本人还是个菜鸟,下面是我的经验之谈,能解决一些问题,有不对的地方,敬请斧正. 我的是CentOS6.3+MySQL5.1.57. 重启了一次服务器后,使用> mysql -u root -p登陆 ...

  10. Lambda应用模式

    Lambda应用模式 前言 在使用 Lambda 表达式时,我们常会碰到一些典型的应用场景,而从常用场景中抽取出来的应用方式可以描述为应用模式.这些模式可能不全是新的模式,有的参考自 JavaScri ...