1、引入依赖

<dependency>
<groupId>pinyin4j.sourceforge.net</groupId>
<artifactId>pinyin4j</artifactId>
<version>2.5.0</version> </dependency>

2、代码

package net.biocloud.social.management.utils;

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; import org.slf4j.Logger;
import org.slf4j.LoggerFactory; public class ChineseToEnglishHelper { private static final Logger LOG = LoggerFactory.getLogger(ChineseToEnglishHelper.class); /**
* 姓名转换为拼音,如胡八一, BaYi Hu
*
* @param realName
* @return
*/
public static String chineseToPinyin(String realName) {
StringBuffer pybf = new StringBuffer();
char[] arr = realName.trim().toCharArray();
HanyuPinyinOutputFormat defaultFormat = new HanyuPinyinOutputFormat();
defaultFormat.setCaseType(HanyuPinyinCaseType.LOWERCASE);
defaultFormat.setToneType(HanyuPinyinToneType.WITHOUT_TONE);
String surname = "";
for (int i = 0; i < arr.length; i++) {
if (arr[i] > 128) {
try {
//单个汉字转为拼音
String[] _t = PinyinHelper.toHanyuPinyinStringArray(arr[i], defaultFormat);
if (i == 0) {
surname = _t[0].substring(0, 1).toUpperCase() + _t[0].substring(1, _t[0].length());
} else {
pybf.append(_t[0].substring(0, 1).toUpperCase() + _t[0].substring(1, _t[0].length()));
}
if (i == arr.length - 1) {
pybf.append(" " + surname);
}
} catch (BadHanyuPinyinOutputFormatCombination e) {
LOG.info("chinese to english error, this is message" + e.getMessage());
throw new RuntimeException("chinese to english error");
}
} else {
pybf.append(arr[i]);
}
}
return pybf.toString().trim();
} /**
* 姓名转换为拼音,如胡八一, B Y Hu
*
* @param realName
* @return
*/
public static String chineseToEnglish(String realName) {
StringBuffer pybf = new StringBuffer();
char[] arr = realName.trim().toCharArray();
HanyuPinyinOutputFormat defaultFormat = new HanyuPinyinOutputFormat();
defaultFormat.setCaseType(HanyuPinyinCaseType.LOWERCASE);
defaultFormat.setToneType(HanyuPinyinToneType.WITHOUT_TONE);
String surname = "";
for (int i = 0; i < arr.length; i++) {
if (arr[i] > 128) {
try {
//单个汉字转为拼音
String[] _t = PinyinHelper.toHanyuPinyinStringArray(arr[i], defaultFormat);
if (i == 0) {
surname = _t[0].substring(0, 1).toUpperCase() + _t[0].substring(1, _t[0].length());
} else {
pybf.append((arr.length == 2) ? _t[0].substring(0, 1).toUpperCase() :
(i == 1) ? _t[0].substring(0, 1).toUpperCase() + " " : _t[0].substring(0, 1).toUpperCase());
}
if (i == arr.length - 1) {
pybf.append(" " + surname);
}
} catch (BadHanyuPinyinOutputFormatCombination e) {
LOG.info("chinese to pinyin error, this is message" + e.getMessage());
throw new RuntimeException("chinese to pinyin error");
}
} else {
pybf.append(arr[i]);
}
}
return pybf.toString().trim();
} public static void main(String arg[]) {
System.out.println(chineseToPinyin("胡八一"));
System.out.println(chineseToEnglish("胡八一"));
} }

汉字转拼音(pinyin4j)的更多相关文章

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

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

  2. 汉字转拼音 pinyin4j 字符串 MD

    Markdown版本笔记 我的GitHub首页 我的博客 我的微信 我的邮箱 MyAndroidBlogs baiqiantao baiqiantao bqt20094 baiqiantao@sina ...

  3. 汉字转拼音Pinyin4j工具(C#、Java都可用)

    C#用法: string pinyin=GetStringPinYin("张三"); //方法如下 public static string GetStringPinYin(str ...

  4. 最好用的汉字转拼音代码PinYin4Objc(PinYin4J的objc版本)

    转:https://github.com/kimziv/PinYin4Objc 最好用的汉字转拼音代码PinYin4Objc(PinYin4J的objc版本)(更新到v1.1.1,增加block异步处 ...

  5. 汉字转拼音,TinyPinyin、Pinyin4j与JPinyin哪个库更快

    1. 介绍 本文对TinyPinyin.Pinyin4j与JPinyin三个汉字转拼音库的用法.测试代码及转换的结果做一个简单的总结. TinyPinyin 适用于Java和Android的快速.低内 ...

  6. Java通过pinyin4j实现汉字转拼音

       碰到个需求,需要按用户名字的首字母来排序.这就需要获取汉字对应的拼音了,突然就想起了pinyin4j这个jar包,于是就开始写了个汉字转拼音的工具类.在此记录一下,方便后续查阅 一.Pom依赖 ...

  7. 汉字转拼音,TinyPinyin、Pinyin4j与JPinyin哪个库更快

    1. 介绍 本文对TinyPinyin.Pinyin4j与JPinyin三个汉字转拼音库的用法.测试代码及转换的结果做一个简单的总结. TinyPinyin 适用于Java和Android的快速.低内 ...

  8. Java汉字转拼音

    import net.sourceforge.pinyin4j.PinyinHelper; import net.sourceforge.pinyin4j.format.HanyuPinyinCase ...

  9. java实现将汉字转为拼音

    有时候为了方便操作程序的开发,需要将汉字转为拼音等操作.下面这个是自己结合网上的资料,加上自己在公司项目中的亲自实践.完整的实现了将汉字转为拼音的操作.这个Demo只是负责将其转换,在main方法中测 ...

随机推荐

  1. Delicious Retouch 3

    今天发现一个photoshop的插件:Delicious Retouch 3,磨皮的,特好用,各种磨皮方法的合集.今后都不敢说自己会磨皮了. 插件的界面 插件的使用教程 链接:http://pan.b ...

  2. Python 培训之MySql

    1. Install  1.1 install mysql sudo apt-get install lamp-server^ (tip: Set password) 1.2 install MySQ ...

  3. [转]图片中的字符分割提取(基于opencv)

    http://blog.csdn.net/anqing715/article/details/16883863 源图片 像这些图片的字符就比较好操作,每个字符都独立,不连在一起,所以轮廓检测最好了.所 ...

  4. WinRAR压缩

    WinRAR压缩软件: ------------------ 软件官网:http://www.winrar.com.cn/ -------------------------------

  5. Java数据结构——栈

    //================================================= // File Name : Stack_demo //-------------------- ...

  6. PHP内存溢出解决方案

    一.内存溢出解决方案 在做数据统计分析时,经常会遇到大数组,可能会发生内存溢出,这里分享一下我的解决方案.还是用例子来说明这个问题,如下: 假定日志中存放的记录数为500000条,那么解决方案如下: ...

  7. notification的使用

    示例: NotificationManager nm = (NotificationManager)getSystemService(Context.NOTIFICATION_SERVICE); No ...

  8. VirtualBox5中安装的CentOS6.7安装增强工具

    1.安装编译内核的相关组件 yum install kernel-devel gcc 2.安装VirtualBox增强功能 sh ./VBoxLinuxAdditions.run 3.重启系统 reb ...

  9. VIM自动补全插件 - YouCompleteMe--"大神级vim补全插件"

    VIM自动补全插件 - YouCompleteMe 序言 vim 之所以被称为编辑器之神多半归功于其丰富的可DIY的灵活插件功能,( 例如vim下的这款神级般的代码补全插件YouCompleteMe) ...

  10. IIS Express 虚拟目录

    1.打开C:\Users\<用户名>\Documents\IISExpress\config\applicationhost.config 2.编辑site节如下(行4) [html] v ...