使用pinyin4j实现汉字转拼音
1. maven项目,请在pom.xml里边添加包依赖相关配置:
<dependency>
<groupId>net.sourceforge.pinyin4j</groupId>
<artifactId>pinyin4j</artifactId>
<version>2.5.0</version>
</dependency>
2.编写实例代码:
/*
* Copyright 2013 Alibaba.com All right reserved. This software is the
* confidential and proprietary information of Alibaba.com ("Confidential
* Information"). You shall not disclose such Confidential Information and shall
* use it only in accordance with the terms of the license agreement you entered
* into with Alibaba.com.
*/
package com.yunos.tv.server.controller.web; import net.sourceforge.pinyin4j.PinyinHelper;
import net.sourceforge.pinyin4j.format.HanyuPinyinOutputFormat;
import net.sourceforge.pinyin4j.format.HanyuPinyinToneType;
import net.sourceforge.pinyin4j.format.HanyuPinyinVCharType;
import net.sourceforge.pinyin4j.format.exception.BadHanyuPinyinOutputFormatCombination; import org.junit.Test; /**
* 类PinyinTest.java的实现描述:TODO 类实现描述
* @author riqi 2013-6-28 下午5:24:57
*/
public class PinyinTest { @Test
public void pinyinTest() { String input = "阿里巴巴";
StringBuilder pinyin = new StringBuilder(); for (int i = 0; i < input.length(); i++) {
HanyuPinyinOutputFormat defaultFormat = new HanyuPinyinOutputFormat();
defaultFormat.setToneType(HanyuPinyinToneType.WITHOUT_TONE);
defaultFormat.setVCharType(HanyuPinyinVCharType.WITH_V);
char c = input.charAt(i);
String[] pinyinArray = null;
try {
pinyinArray = PinyinHelper.toHanyuPinyinStringArray(c, defaultFormat);
} catch (BadHanyuPinyinOutputFormatCombination e) {
e.printStackTrace();
}
if (pinyinArray != null) {
pinyin.append(pinyinArray[0]);
} else if (c != ' ') {
pinyin.append(input.charAt(i));
}
} System.out.println(pinyin.toString().trim().toLowerCase());
} }
3. 运行结果:alibaba
使用pinyin4j实现汉字转拼音的更多相关文章
- Java通过pinyin4j实现汉字转拼音
碰到个需求,需要按用户名字的首字母来排序.这就需要获取汉字对应的拼音了,突然就想起了pinyin4j这个jar包,于是就开始写了个汉字转拼音的工具类.在此记录一下,方便后续查阅 一.Pom依赖 ...
- [转]Java汉字按照拼音排序
最近项目上使用到汉字排序的问题,网上搜索了一下后普遍使用下面的方法比较. @Test public void test_sort_pinyin() { Collator cmp = Collator. ...
- 汉字转拼音(pinyin4j)
1.引入依赖 <dependency> <groupId>pinyin4j.sourceforge.net</groupId> <artifactId> ...
- 最好用的汉字转拼音代码PinYin4Objc(PinYin4J的objc版本)
转:https://github.com/kimziv/PinYin4Objc 最好用的汉字转拼音代码PinYin4Objc(PinYin4J的objc版本)(更新到v1.1.1,增加block异步处 ...
- 汉字转拼音,TinyPinyin、Pinyin4j与JPinyin哪个库更快
1. 介绍 本文对TinyPinyin.Pinyin4j与JPinyin三个汉字转拼音库的用法.测试代码及转换的结果做一个简单的总结. TinyPinyin 适用于Java和Android的快速.低内 ...
- JAVA实现汉字转换为拼音 pinyin4j/JPinyin
在项目中经常会遇到需求用户输入汉字后转换为拼音的场景,比如说通讯录,就会要求按名字首字符发音排序,如果自己写实现这方面的功能是个很好大的工程,还好网上有公开的第三方jar支持转换,结合网上很多前辈的代 ...
- 汉字转拼音,TinyPinyin、Pinyin4j与JPinyin哪个库更快
1. 介绍 本文对TinyPinyin.Pinyin4j与JPinyin三个汉字转拼音库的用法.测试代码及转换的结果做一个简单的总结. TinyPinyin 适用于Java和Android的快速.低内 ...
- 通过pinyin4j将汉字转换为全拼 和 拼音首字母
/** * 汉字转换为拼音 包含多音字,包含生母zh,ch,sh的 */ public void toPinYinAll(){ String initials = "zh,ch,sh&quo ...
- Java汉字转拼音
import net.sourceforge.pinyin4j.PinyinHelper; import net.sourceforge.pinyin4j.format.HanyuPinyinCase ...
随机推荐
- ognl概念和原理详解
一.问题的提出 在mvc中,数据是在各个层次之间进行流转是一个不争的事实.而这种流转,也就会面临一些困境,这些困境,是由于数据在不同世界中的表现形式不同而造成的: 1. 数据在页面上是一个扁平的, ...
- 12. Min Stack【medium】
Implement a stack with min() function, which will return the smallest number in the stack. It should ...
- 【C语言】18-变量类型
一.变量的作用域 C语言根据变量作用域的不同,将变量分为局部变量和全局变量. 1.局部变量 1> 定义:在函数内部定义的变量,称为局部变量.形式参数也属于局部变量. 2> 作用域:局部变量 ...
- 出现“unrecognized selector sent to instance”问题原因之一及解决方法。
对于iPhone开发初学者来说,很想实现自己在iPhone上的第一个小程序,准备工作就绪侯就信心满满的开始了!一般来说大家可能都是从Hello World做起吧. 反正我是的,:),如果按照文 ...
- ibatis时间比较大小
<![CDATA[ A.RFID_Time >= #StartTime# ]]>时间搜索功能 A.RFID_Time <![CDATA[ ...
- 一些lua代码
1.把k--v表转化为数组表,只支持2级 2.取中值 3.字符串按每行最多n像素分割,并返回每行最大宽度,可以用"\n"手动换行
- QT 5.7.0 移植之 tslib 编译配置
QT5.7 编译请参考:http://www.cnblogs.com/chenfulin5/p/5798764.html 最新的 tslib 是从他的 github 下载下来的. 地址是:https: ...
- PHP——数组2(数组函数,二维数组,正则表达式)
<body> <?php //数组函数 $arr=array(1,2,3,4,5,6); print_r($arr); echo "<br />"; ...
- 数据库递归查询(CET)
IF OBJECT_ID('[ta]') IS NOT NULL DROP TABLE [ta] Go CREATE TABLE ta([id] INT,[name] NVARCHAR(4) ...
- jvm 调整tomcat的堆内存和常驻内存catalina.sh
4.2 性能优化 tomcat性能取决于你的内存大小 上策:优化代码 中策:jvm优化机制 垃圾回收机制 把不需要的内存回收 优化jvm--优化垃圾回收策略 优化catalina.sh配置文件. ...