Pinyin4j之Pinyin4jUtils工具类
介绍
Pinyin4j是一个流行的Java库,支持中文字符和拼音之间的转换,拼音输出格式可以定制。
Pinyin4jUtils工具类是进行包装,进行支持一一些转换方法
支持方法
输入一个字符串可以给我转成首字母返回给我
例如:颐和园
- 转换格式:全部大小YHY
- 转换格式:全部大写(中间加字符串*)Y*H*Y
- 转换格式:全部小写yhy
- 转换格式:全部小写(中间加字符串*)y*h*y
- 转换格式:返回首字母大写Y
- 转换格式:返回首字母小写y
使用方法
Pinyin4j pinyin4j = new Pinyin4j();
String first1 = pinyin4j.toPinYinUppercase("颐和园");
String first2 = pinyin4j.toPinYinUppercase("颐和园", "**");
String first3 = pinyin4j.toPinYinLowercase("颐和园");
String first4 = pinyin4j.toPinYinLowercase("颐和园","**");
String first5 = pinyin4j.toPinYinUppercaseInitials("颐和园");
String first6 = pinyin4j.toPinYinLowercaseInitials("颐和园");
System.out.println(first1); //输出结果:YHY
System.out.println(first2); //输出结果:Y**H**Y
System.out.println(first3); //输出结果:yhy
System.out.println(first4); //输出结果:y**h**y
System.out.println(first5); //输出结果:Y
System.out.println(first6); //输出结果:y
java工具类代码
import org.junit.Test;
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;
/*** 汉字转换成拼音方法* @author 蔡龙*/public class Pinyin4j {HanyuPinyinOutputFormat format = null; public static enum Type { UPPERCASE, //全部大写 LOWERCASE, //全部小写 FIRSTUPPER //首字母大写} public Pinyin4j(){ format = new HanyuPinyinOutputFormat(); format.setCaseType(HanyuPinyinCaseType.UPPERCASE); format.setToneType(HanyuPinyinToneType.WITHOUT_TONE); } /*** 转换全部大写 * @param str 字符串* @return str为颐和园 ,return获取到的是YHY* @throws BadHanyuPinyinOutputFormatCombination*/public String toPinYinUppercase(String str) throws BadHanyuPinyinOutputFormatCombination{ return toPinYin(str, "", Type.UPPERCASE); } /*** 转换全部大写* @param str 字符串* @param spera 转换字母间隔加的字符串,如果不需要为""* @return str为颐和园 ,spera为** return获取到的是Y**H**Y* @throws BadHanyuPinyinOutputFormatCombination*/public String toPinYinUppercase(String str,String spera) throws BadHanyuPinyinOutputFormatCombination{ return toPinYin(str, spera, Type.UPPERCASE); } /*** 转换全部小写* @param str 字符串* @return str为颐和园 ,return获取到的是yhy* @throws BadHanyuPinyinOutputFormatCombination*/public String toPinYinLowercase(String str) throws BadHanyuPinyinOutputFormatCombination{ return toPinYin(str, "", Type.LOWERCASE); } /*** 转换全部小写* @param str 字符串* @param spera 转换字母间隔加的字符串,如果不需要为""* @return str为颐和园 ,spera为** return获取到的是y**h**y* @throws BadHanyuPinyinOutputFormatCombination*/public String toPinYinLowercase(String str,String spera) throws BadHanyuPinyinOutputFormatCombination{ return toPinYin(str, spera, Type.LOWERCASE); } /** * 获取拼音首字母(大写)* @param str 字符串* @return str为颐和园 ,return获取到的是Y* @throws BadHanyuPinyinOutputFormatCombination 异常信息*/ public String toPinYinUppercaseInitials(String str) throws BadHanyuPinyinOutputFormatCombination { String initials = null;String py = toPinYinUppercase(str); if(py.length()>1){initials = py.substring(0, 1);}if(py.length()<=1){initials = py;}return initials.trim(); } /** * 获取拼音首字母(小写)* @param str 字符串* @return str为颐和园 ,return获取到的是y* @throws BadHanyuPinyinOutputFormatCombination 异常信息*/ public String toPinYinLowercaseInitials(String str) throws BadHanyuPinyinOutputFormatCombination { String initials = null;String py = toPinYinLowercase(str); if(py.length()>1){initials = py.substring(0, 1);}if(py.length()<=1){initials = py;}return initials.trim(); } /** * 将str转换成拼音,如果不是汉字或者没有对应的拼音,则不作转换 * @param str 字符串* @param spera 默认,可为""* @param type 转换格式* @return 按照转换格式转换成字符串* @throws BadHanyuPinyinOutputFormatCombination 异常信息 */ public String toPinYin(String str, String spera, Type type) throws BadHanyuPinyinOutputFormatCombination { if(str == null || str.trim().length()==0) { return ""; }if(type == Type.UPPERCASE) { format.setCaseType(HanyuPinyinCaseType.UPPERCASE);} else{ format.setCaseType(HanyuPinyinCaseType.LOWERCASE);}String py = ""; String temp = ""; String[] t; for(int i=0;i<str.length();i++){ char c = str.charAt(i); if((int)c <= 128) {py += c; }else{ t = PinyinHelper.toHanyuPinyinStringArray(c, format); if(t == null) { py += c; }else{ temp = t[0]; if(type == Type.FIRSTUPPER) { temp = t[0].toUpperCase().charAt(0)+temp.substring(1); }if(temp.length()>=1){temp = temp.substring(0, 1);}py += temp+(i==str.length()-1?"":spera); } } } return py.trim(); }
}HanyuPinyinOutputFormat format = null; public static enum Type { UPPERCASE, //全部大写 LOWERCASE, //全部小写 FIRSTUPPER //首字母大写} public Pinyin4j(){ format = new HanyuPinyinOutputFormat(); format.setCaseType(HanyuPinyinCaseType.UPPERCASE); format.setToneType(HanyuPinyinToneType.WITHOUT_TONE); } /*** 转换全部大写 * @param str 字符串* @return str为颐和园 ,return获取到的是YHY* @throws BadHanyuPinyinOutputFormatCombination*/public String toPinYinUppercase(String str) throws BadHanyuPinyinOutputFormatCombination{ return toPinYin(str, "", Type.UPPERCASE); } /*** 转换全部大写* @param str 字符串* @param spera 转换字母间隔加的字符串,如果不需要为""* @return str为颐和园 ,spera为** return获取到的是Y**H**Y* @throws BadHanyuPinyinOutputFormatCombination*/public String toPinYinUppercase(String str,String spera) throws BadHanyuPinyinOutputFormatCombination{ return toPinYin(str, spera, Type.UPPERCASE); } /*** 转换全部小写* @param str 字符串* @return str为颐和园 ,return获取到的是yhy* @throws BadHanyuPinyinOutputFormatCombination*/public String toPinYinLowercase(String str) throws BadHanyuPinyinOutputFormatCombination{ return toPinYin(str, "", Type.LOWERCASE); } /*** 转换全部小写* @param str 字符串* @param spera 转换字母间隔加的字符串,如果不需要为""* @return str为颐和园 ,spera为** return获取到的是y**h**y* @throws BadHanyuPinyinOutputFormatCombination*/public String toPinYinLowercase(String str,String spera) throws BadHanyuPinyinOutputFormatCombination{ return toPinYin(str, spera, Type.LOWERCASE); } /** * 获取拼音首字母(大写)* @param str 字符串* @return str为颐和园 ,return获取到的是Y* @throws BadHanyuPinyinOutputFormatCombination 异常信息*/ public String toPinYinUppercaseInitials(String str) throws BadHanyuPinyinOutputFormatCombination { String initials = null;String py = toPinYinUppercase(str); if(py.length()>1){initials = py.substring(0, 1);}if(py.length()<=1){initials = py;}return initials.trim(); } /** * 获取拼音首字母(小写)* @param str 字符串* @return str为颐和园 ,return获取到的是y* @throws BadHanyuPinyinOutputFormatCombination 异常信息*/ public String toPinYinLowercaseInitials(String str) throws BadHanyuPinyinOutputFormatCombination { String initials = null;String py = toPinYinLowercase(str); if(py.length()>1){initials = py.substring(0, 1);}if(py.length()<=1){initials = py;}return initials.trim(); } /** * 将str转换成拼音,如果不是汉字或者没有对应的拼音,则不作转换 * @param str 字符串* @param spera 默认,可为""* @param type 转换格式* @return 按照转换格式转换成字符串* @throws BadHanyuPinyinOutputFormatCombination 异常信息 */ public String toPinYin(String str, String spera, Type type) throws BadHanyuPinyinOutputFormatCombination { if(str == null || str.trim().length()==0) { return ""; }if(type == Type.UPPERCASE) { format.setCaseType(HanyuPinyinCaseType.UPPERCASE);} else{ format.setCaseType(HanyuPinyinCaseType.LOWERCASE);}String py = ""; String temp = ""; String[] t; for(int i=0;i<str.length();i++){ char c = str.charAt(i); if((int)c <= 128) {py += c; }else{ t = PinyinHelper.toHanyuPinyinStringArray(c, format); if(t == null) { py += c; }else{ temp = t[0]; if(type == Type.FIRSTUPPER) { temp = t[0].toUpperCase().charAt(0)+temp.substring(1); }if(temp.length()>=1){temp = temp.substring(0, 1);}py += temp+(i==str.length()-1?"":spera); } } } return py.trim(); }
}
Pinyin4j之Pinyin4jUtils工具类相关推荐
- Pinyin4j 的使用 Pinyin4jUtils工具类
官网 功能 1.支持简体中文和繁体中文字符. 2.支持转换到汉语拼音,通用拼音, 威妥玛拼音(威玛拼法), 注音符号第二式, 耶鲁拼法和国语罗马字. 3.支持多音字,即可以获取一个中文字符的多种发音. ...
- pinyin4j工具类
pinyin4j工具类 1. 导入依赖 <dependency><groupId>com.belerweb</groupId><artifactId>p ...
- 汉字转拼音工具类,依赖Pinyin4J
汉字转拼音工具类,依赖Pinyin4J Maven 坐标 <dependency><groupId>com.belerweb</groupId><artifa ...
- 汉字转拼音工具类pinyin4j
依赖jar包 <dependency><groupId>com.belerweb</groupId><artifactId>pinyin4j</a ...
- java pinyin4j.jar 下载_Java pinyin4j 工具类实现代码分享
工具类代码:package com.what21.util.pinyin; import net.sourceforge.pinyin4j.PinyinHelper; import net.sourc ...
- pinyin4j汉字转换拼音的工具类
import net.sourceforge.pinyin4j.PinyinHelper;/*** 功能说明:汉字转换拼音工具类* * @author LZG*/ public class Pinyi ...
- Java实用工具类-将汉字转为拼音
1. 导入对应的jar包 <dependency><groupId>com.belerweb</groupId><artifactId>pinyin4j ...
- java 中文转拼音_JAVA 将中文转化为拼音工具类
将pinyin4j-2.5.0.jar (附件中有,记得解压)加入到classpath中. 工具类PinyinUtils代码见下:package com.test.utils; import net. ...
- 汉字转换为拼音Java工具类
声明:本文是对https://www.cnblogs.com/DreamDrive/p/5762078.html的整理,感谢博主分享. 我就是爱音乐~ 准备工作:在pom.xml中引入依赖. < ...
最新文章
- 使用python连接数据库
- AI:人工智能概念之机器学习中常用算法的思维导图集合(非常经典、建议收藏)之详细攻略
- 云起实验室有奖征文,精美好礼等你来领——《我的linux初体验》
- 一文带你了解华为云DevCloud为何能全面领跑中国DevOps云服务市场
- spark1.6.1 on yarn搭建部署
- html将两张图重叠居中代码,【CSS技巧】多图片的垂直居中排版
- python中run函数作用_Python run()函数和start()函数的比较和差别介绍
- 请求头User-Agent作用?
- 研发项目管理软件对比调研报告
- 单片机外文参考文献期刊_单片机_英文参考文献..doc
- 云台测试软件,三轴云台校准工具
- 海信信号机后台服务器配置,海信自适应交通信号控制系统解决方案
- 蓝牙打印机CPCL编程手册~汉印HM-A300
- Mac中隐藏/显示文件或文件夹
- Flowable 流程实例
- MFC工具箱中各种控件
- JavaScript Lodash 工具库
- 全球与中国手持式凿岩机市场供需情况分析及十四五趋势预测报告2022-2027年版
- Android刘海屏适配全方案(华为、小米、Vivo、Oppo)
- html5无序列表,有序列表,定义列表,组合标签,div分区标签