引入maven依赖
<dependencies>
<dependency>
<groupId>com.belerweb</groupId>
<artifactId>pinyin4j</artifactId>
<version>2.5.0</version>
</dependency>
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; import java.io.UnsupportedEncodingException; public class CNUtil {
/**
* 将汉字转换为全拼
*
* @param src
* @return String
*/
public static String getPinYin(String src) {
char[] t1 = null;
t1 = src.toCharArray();
// System.out.println(t1.length);
String[] t2 = new String[t1.length];
// System.out.println(t2.length);
// 设置汉字拼音输出的格式
HanyuPinyinOutputFormat t3 = new HanyuPinyinOutputFormat();
t3.setCaseType(HanyuPinyinCaseType.LOWERCASE);
t3.setToneType(HanyuPinyinToneType.WITHOUT_TONE);
t3.setVCharType(HanyuPinyinVCharType.WITH_V);
String t4 = "";
int t0 = t1.length;
try {
for (int i = 0; i < t0; i++) {
// 判断是否为汉字字符
// System.out.println(t1[i]);
if (Character.toString(t1[i]).matches("[\\u4E00-\\u9FA5]+")) {
t2 = PinyinHelper.toHanyuPinyinStringArray(t1[i], t3);// 将汉字的几种全拼都存到t2数组中
if(t2 != null){
t4 += t2[0];// 取出该汉字全拼的第一种读音并连接到字符串t4后
}
} else {
// 如果不是汉字字符,直接取出字符并连接到字符串t4后
t4 += Character.toString(t1[i]);
}
}
} catch (BadHanyuPinyinOutputFormatCombination e) {
// TODO Auto-generated catch block
e.printStackTrace();
//t4 = "abc";
}
return t4;
} /**
* 提取每个汉字的首字母
*
* @param str
* @return String
*/
public static String getPinYinHeadChar(String str) {
String convert = "";
for (int j = 0; j < str.length(); j++) {
char word = str.charAt(j);
// 提取汉字的首字母
String[] pinyinArray = PinyinHelper.toHanyuPinyinStringArray(word);
if (pinyinArray != null) {
convert += pinyinArray[0].charAt(0);
} else {
convert += word;
}
}
return convert;
} /**
* 将字符串转换成ASCII码
*
* @param cnStr
* @return String
*/
public static String getCnASCII(String cnStr) {
StringBuffer strBuf = new StringBuffer();
// 将字符串转换成字节序列
byte[] bGBK = cnStr.getBytes();
for (int i = 0; i < bGBK.length; i++) {
// System.out.println(Integer.toHexString(bGBK[i] & 0xff));
// 将每个字符转换成ASCII码
strBuf.append(Integer.toHexString(bGBK[i] & 0xff)+" ");
}
return strBuf.toString();
} public static String getFirstUpperCase(String str){
String buf = getPinYin(str).substring(0, 1); return buf.toUpperCase();
} /**
* 获得GBK编码的字符串长度
* @param value
* @return
*/
public static int getGBKLength(String value){
if(value==null){
return 0;
}
try {
return value.getBytes("GBK").length;
} catch (UnsupportedEncodingException e) {
return 0;
}
}
public static void main(String[] args) {
String cnStr = "中华人民共和国";
System.out.println(getPinYin(cnStr));
System.out.println(getPinYinHeadChar(cnStr));
System.out.println(getFirstUpperCase(cnStr));
// System.out.println(getCnASCII(cnStr));
System.out.println(String.format("%06d", 1));
} }

使用pinyin4j汉字转pinyin的更多相关文章

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

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

  2. solr入门之pinyin4j源代码改写动态加入扩展词及整合进war项目中

    1.初始化时载入用户定义的字典 package net.sourceforge.pinyin4j; import net.sourceforge.pinyin4j.multipinyin.Trie; ...

  3. JAVA实现汉字转拼音

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

  4. 基于SolrCloud的内容搜索和热点推送

    ➠更多技术干货请戳:听云博客 什么是热点 我认为热点有时效性和受众面 用户关注从低到高再到低的内容 .有公共热点和分类热点.例如医辽养老全民关注,科技汽车等只有特定的人群关注. 推送的条件 搜索频次达 ...

  5. 收藏的技术文章链接(ubuntu,python,android等)

    我的收藏 他山之石,可以攻玉 转载请注明出处:https://ahangchen.gitbooks.io/windy-afternoon/content/ 开发过程中收藏在Chrome书签栏里的技术文 ...

  6. solr入门之搜索建议的几种实现方式和最终选取实现思路

    上篇博客中我简单的讲了下solr自身的suggest模块来实现搜索建议.但是今天研究了下在solr自身的suggest中添加进去拼音来智能推荐时不时很方便.在次从网上搜集和整理思考了下该问题的解决. ...

  7. jar插件应用

    Gson(解析json) 作用:在servlet层中解析json 1:导入jar包 gson-2.2.4.jar 例如:Gson gson = new Gson();                  ...

  8. 搜索引擎keyword智能提示的一种实现

    问题背景 搜索关键字智能提示是一个搜索应用的标配.主要作用是避免用户输入错误的搜索词,并将用户引导到相应的关键词上,以提升用户搜索体验. 美团CRM系统中存在数以百万计的商家,为了让用户高速查找到目标 ...

  9. [转]网易云音乐Android版使用的开源组件

    原文链接 网易云音乐Android版从第一版使用到现在,全新的 Material Design 界面,更加清新.简洁.同样也是音乐播放器开发者,我们确实需要思考,相同的功能,会如何选择.感谢开源,让我 ...

随机推荐

  1. MySql 查询数据库中所有表名以及对比分布式库中字段和表的不同

    查询数据库中所有表名select table_name from information_schema.tables where table_schema='数据库名' and table_type= ...

  2. Google maps api demo 2

    demo /** * @fileoverview Sample showing capturing a KML file click * and displaying the contents in ...

  3. elasticsearch 拼音搜索

    现在很多公司都开始使用es来做搜索,我们公司目前也有好几个业务部门在用,我主要做商户搜索,为业务部门提供基础支持.上周把呼叫中心的搜索重新整理了下,在新增几个字段后,全量同步发现通过拼音首字母搜索无法 ...

  4. Python caffe.TEST Example(Demo)

    下面提供了caffe python的六个测试demo,大家可以根据自己的需求进行修改. Example 1 From project FaceDetection_CNN-master, under d ...

  5. JVM 深入浅出

    jvm 相信大家都有认知 在面试中也是经常遇见的希望接下来的这篇文章可以对你们带来帮助 java 的好处 : java这门语言之所以受广大的码农所喜爱是因为它不像C语言需要程序员自己手动分配内存空间 ...

  6. 一些官方的github地址

    阿里巴巴开源github地址:https://github.com/alibaba 腾讯开源github地址:https://github.com/Tencent 奇虎360github地址:http ...

  7. numpy 矩阵归一化

    new_value = (value - min)/(max-min) def normalization(datingDatamat): max_arr = datingDatamat.max(ax ...

  8. Educational Codeforces Round 22E

    给你n和k,n个数,每个数范围1e5,m次查询,每次查询区间(l,r),在区间中的每个数,如果超过k次只算k次,否则算原来的次数,求总次数,强制在线 解法:线段树维护区间中每个数经过k次到达的点pos ...

  9. lightoj1138

    二分 #include<map> #include<set> #include<cmath> #include<queue> #include<s ...

  10. Xcode 8 GM 编译缺失 /Users/usr/lib/libresolv.9.dylib

    原因是操作系统的文件与手机需要的不同. 解决办法是将iOS DeviceSupport里当前手机版本的Symbols的libresolv.9.dylib文件,代替编译失败项目的Build Phases ...