java util - 中文、繁体转成拼音工具pinyin4j
需要 pinyin4j-2.5.0.jar 包
代码例子
package cn.java.pinyin4j; 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.HanyuPinyinVCharType;
import net.sourceforge.pinyin4j.format.exception.BadHanyuPinyinOutputFormatCombination; /**
* pinyin4j是一个支持将简体和繁体中文转换到成拼音的Java开源类库
*/
public class Test { public static void main(String[] args) {
System.out.println("中文a1".toCharArray().length); // = 4
String chs = "我是中国人! I'm Chinese!";
System.out.println(chs);
System.out.println(getPinYin1(chs));
System.out.println(getPinYin2(chs));
} public static String getPinYin1(String inputString) { HanyuPinyinOutputFormat format = new HanyuPinyinOutputFormat();
format.setCaseType(HanyuPinyinCaseType.LOWERCASE);
format.setToneType(HanyuPinyinToneType.WITH_TONE_MARK);
format.setVCharType(HanyuPinyinVCharType.WITH_U_UNICODE); char[] input = inputString.trim().toCharArray();
StringBuffer output = new StringBuffer(""); try {
for (int i = 0; i < input.length; i++) {
if (Character.toString(input[i]).matches("[\u4E00-\u9FA5]+")) {
String[] temp = PinyinHelper.toHanyuPinyinStringArray(input[i], format);
output.append(temp[0]);
output.append(" ");
} else
output.append(Character.toString(input[i]));
}
} catch (BadHanyuPinyinOutputFormatCombination e) {
e.printStackTrace();
}
return output.toString();
} public static String getPinYin2(String inputString) { HanyuPinyinOutputFormat format = new HanyuPinyinOutputFormat();
format.setCaseType(HanyuPinyinCaseType.LOWERCASE);
format.setToneType(HanyuPinyinToneType.WITHOUT_TONE);
format.setVCharType(HanyuPinyinVCharType.WITH_U_UNICODE); char[] input = inputString.trim().toCharArray();
StringBuffer output = new StringBuffer(""); try {
for (int i = 0; i < input.length; i++) {
if (Character.toString(input[i]).matches("[\u4E00-\u9FA5]+")) {
String[] temp = PinyinHelper.toHanyuPinyinStringArray(input[i], format);
output.append(temp[0]);
output.append(" ");
} else
output.append(Character.toString(input[i]));
}
} catch (BadHanyuPinyinOutputFormatCombination e) {
e.printStackTrace();
}
return output.toString();
}
}
输出结果
4
我是中国人! I'm Chinese!
wŏ shì zhōng guó rén ! I'm Chinese!
wo shi zhong guo ren ! I'm Chinese!
java util - 中文、繁体转成拼音工具pinyin4j的更多相关文章
- 使用 js 实现一个中文自动转换成拼音的工具库
使用 js 实现一个中文自动转换成拼音的工具库 中文 => zhong-wen 应用场景 SEO 友好, URL 自动转换 blogs 发布文章,自动化部署,自动生成 url 的 path (时 ...
- java中汉字自动转换成拼音
java中汉字自动转换成拼音 1.需要下载jar包 pinyin4j.2.5.0.jar ,加入到WEB-INF下的lib里边,右键add to bulid path. 2.[代码]PinYinUti ...
- java获取中文汉字的所有拼音
java获取中文汉字的所有拼音 中文汉字可能有很多读音,java中分别用1,2,3,4来区别,例如“作”字,就有三个读音,zuo1,zuo2,zuo4. java获取汉字读音拼音代码如下所示: S ...
- java util - MD5/AES/RSA快速调用工具
测试代码 package cn.java.security; import java.security.Key; import java.util.Base64; import org.junit.A ...
- Java.util.Math类--数学相关的工具类
Math类--数学相关的工具类 java.util.Math类是数学相关的工具类,里面提供了大量的静态方法,完成与数学运算相关的操作. public static double abs(double ...
- [转载] java多线程学习-java.util.concurrent详解(一) Latch/Barrier
转载自http://janeky.iteye.com/blog/769965 Java1.5提供了一个非常高效实用的多线程包:java.util.concurrent, 提供了大量高级工具,可 ...
- java.util.Arrays,java.lang.Math,java.lang.System 类的常用方法汇总
java.util.Arrays类是数组的工具类,一般数组常用的方法包括 二分查找:public static int binarySearch(array[],int key),返回key的下标i ...
- SnowNLP:•中文分词•词性标准•提取文本摘要,•提取文本关键词,•转换成拼音•繁体转简体的 处理中文文本的Python3 类库
SnowNLP是一个python写的类库,可以方便的处理中文文本内容,是受到了TextBlob的启发而写的,由于现在大部分的自然语言处理库基本都是针对英文的,于是写了一个方便处理中文的类库,并且和Te ...
- Java 中文转换拼音工具
Java 中文转换拼音工具 /** * <html> * <body> * <P> Copyright 1994 JsonInternational</p&g ...
随机推荐
- CAST 变类型,如 SELECT CAST('12' AS int)
select distinct CAST(value as signed) valueprice,CAST(value as signed) valueid from app_coupon
- js执行上下文和执行栈
执行上下文就是JavaScript 在被解析和运行时环境的抽象概念,JavaScript 运行任何代码都是在执行上下文环境中运行的,执行上下文包括三个周期:创建——运行——销毁,重点说一下创建环节. ...
- SVN的搭建(权限配置篇)
如要转载,请注明出处! 两个问题: 如何维护多个目录或者仓库 目录权限如何设置 ============================================================ ...
- Morris.js-利用JavaScript生成时序图
morris.js是一个轻量级的时间序列javascript类库,是网页显示图表的好工具.github项目地址:点击打开,使用起来很简单,但是需要你有一点网页设计的一些基本知识,对一个网页内容的结构要 ...
- agc016B - Colorful Hats(智商题)
题意 题目链接 有$n$个人,每个人有一种颜色,第$i$个人说除了我之外有$a_i$种不同的颜色,问是否存在一组合法解 Sol 700分的题就这么神仙了么..好难啊... 先说结论吧 设$mx, mn ...
- Sometimes it takes going through something so awful to realize the beauty that is out there in this world.
Sometimes it takes going through something so awful to realize the beauty that is out there in this ...
- JS实现正则表达式
一.创建正则表达式 一共有两种方式: 1.直接量:var re = /[0-9]*/; 2.通过RegExp对象的构造函数:var re = RegExp("[0-9]*",&qu ...
- Sqlserver计算本年度工作日
--@StartDate 本年度第一天 --@EndDate 本年度最后一天 , ) , DATEADD(yy, , , )) IF EXISTS ( SELECT * FROM tempdb..sy ...
- 解决mysql连接输入密码提示Warning: Using a password on the command line interface can be insecure
有时候客户端连接mysql需要指定密码时(如用zabbix监控mysql)5.6后数据库会给出个警告信息 mysql -uroot -pxxxx Warning: Using a password o ...
- 集成Ehcache用来缓存表以后,怎么设置缓存刷新时间
问答 集成Ehcache用来缓存表以后,怎么设置缓存刷新时间 发布于 217天前 作者 老司机 93 次浏览 复制 上一个帖子 下一个帖子 标签: 无 集成Ehcache用来缓存表以后, ...