需要 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的更多相关文章

  1. 使用 js 实现一个中文自动转换成拼音的工具库

    使用 js 实现一个中文自动转换成拼音的工具库 中文 => zhong-wen 应用场景 SEO 友好, URL 自动转换 blogs 发布文章,自动化部署,自动生成 url 的 path (时 ...

  2. java中汉字自动转换成拼音

    java中汉字自动转换成拼音 1.需要下载jar包 pinyin4j.2.5.0.jar ,加入到WEB-INF下的lib里边,右键add to bulid path. 2.[代码]PinYinUti ...

  3. java获取中文汉字的所有拼音

    java获取中文汉字的所有拼音   中文汉字可能有很多读音,java中分别用1,2,3,4来区别,例如“作”字,就有三个读音,zuo1,zuo2,zuo4. java获取汉字读音拼音代码如下所示: S ...

  4. java util - MD5/AES/RSA快速调用工具

    测试代码 package cn.java.security; import java.security.Key; import java.util.Base64; import org.junit.A ...

  5. Java.util.Math类--数学相关的工具类

    Math类--数学相关的工具类 java.util.Math类是数学相关的工具类,里面提供了大量的静态方法,完成与数学运算相关的操作. public static double abs(double ...

  6. [转载] java多线程学习-java.util.concurrent详解(一) Latch/Barrier

    转载自http://janeky.iteye.com/blog/769965     Java1.5提供了一个非常高效实用的多线程包:java.util.concurrent, 提供了大量高级工具,可 ...

  7. java.util.Arrays,java.lang.Math,java.lang.System 类的常用方法汇总

    java.util.Arrays类是数组的工具类,一般数组常用的方法包括 二分查找:public static int  binarySearch(array[],int key),返回key的下标i ...

  8. SnowNLP:•中文分词•词性标准•提取文本摘要,•提取文本关键词,•转换成拼音•繁体转简体的 处理中文文本的Python3 类库

    SnowNLP是一个python写的类库,可以方便的处理中文文本内容,是受到了TextBlob的启发而写的,由于现在大部分的自然语言处理库基本都是针对英文的,于是写了一个方便处理中文的类库,并且和Te ...

  9. Java 中文转换拼音工具

    Java 中文转换拼音工具 /** * <html> * <body> * <P> Copyright 1994 JsonInternational</p&g ...

随机推荐

  1. bzoj 3732: Network 树上两点边权最值

    http://www.lydsy.com/JudgeOnline/problem.php?id=3732 首先想到,要使得最长边最短,应该尽量走最短的边,在MST上. 然后像LCA那样倍增娶个最大值 ...

  2. 如何去除Discuz标题栏中的Powered by Discuz!

    今天修改discuz代码遇到一个问题,就是标题栏中的Powered by Discuz!,很不美观.查资料后得到了解决方法!介绍给大家. 那么如何去掉标题里面的Powered by Discuz!呢? ...

  3. js 独立命名空间,私有成员和静态成员

    独立的命名空间   1可以避免全局变量污染. 全局变量污染不是 说 被全局变量污染,而是说不会污染全局变量.   2实现私有成员. 在js中函数 就可以满足独立的命名空间的两点需求.   如:     ...

  4. DBTransaction

    // Created by 张国锋 on 15-7-23. // Copyright (c) 2014年 张国锋. All rights reserved. // #import "AppD ...

  5. SQL语句创建数据库以及一些查询练习

    --创建 MyCompany数据库 use master execute sp_configure 'show advanced options',1 --开启权限 reconfigure execu ...

  6. Java中的for循环——通过示例学习Java编程(9)

      作者:CHAITANYA SINGH 来源:https://www.koofun.com/pro/kfpostsdetail?kfpostsid=21 循环用于反复执行同一组语句,直到满足特定条件 ...

  7. i++ ++i i=i+1 和i+=1

    这几个运算符的差别总是过一段时间就爱搞混,每次需要百度,还是自己记录一下方便查阅. int i=0; System.out.println(i++); 输出:0 int i=0; System.out ...

  8. python基本数据类型,int,bool,str

    一丶python基本数据类型 1.int 整数,主要用来进行数学运算. 2.str 字符串,可以保存少量数据并进行相应的操作 3.bool 判断真假.True.False 4.list 存储大量数据, ...

  9. javascript设计模式之装饰者模式

    /* * 装饰者模式提供比继承更有弹性的替代方案 * 在不改变原构造函数的情况下,添加新的属性或功能 */ //需要装饰的类(函数) function Macbook() { this.cost = ...

  10. linux 后渗透测试

    学习参考: http://weibo.com/1869235073/B9Seswf9R?type=comment http://weibo.com/p/1001603723521007220513 h ...