当前位置:网站首页>Chinese characters to Pinyin
Chinese characters to Pinyin
2022-08-05 02:33:00 【The axe lake lazy】
一,pinyin4j 工具包
We need to use Chinese characters to pinyinpinyin4j这个工具jar包,It can handle converting Chinese into Pinyin(汉语拼音,罗马拼音等)
1,常用类介绍
PinyinHelper Provides the main methods of converting Chinese characters to Pinyin
HanyuPinyinOutputFormat Defines how to output Pinyin,
HanyuPinyinCaseType Provides styles for pinyin output
- LOWERCASE:输出小写,
- UPPERCASE:输出大写
HanyuPinyinToneType Settings for output phonetic symbols
- WITH_TONE_MARK:Use phonetic symbols directly(必须设置WITH_U_UNICODE,否则会抛出异常),
- WITH_TONE_NUMBER:1-4Numbers represent phonetic symbols,
- WITHOUT_TONE:没有音标
HanyuPinyinVCharType special phonetic symbolsü的设置(了解下)
- WITH_V:用v表示ü,
- WITH_U_AND_COLON:用"u:"表示ü,
- WITH_U_UNICODE:直接用ü
二,使用pinyin4j
1,引用jar包
<dependency>
<groupId>com.belerweb</groupId>
<artifactId>pinyin4j</artifactId>
<version>2.5.0</version>
</dependency>
2,写一个工具类
The two methods in the utility class can be combined
/** * @description:汉字转拼音工具类 */
public class HanZiToPinYinUtil {
public static void main(String[] args) {
String allPinYin = getAllPinYin("我是中国人00");
String firstPinYin = getFirstPinYin("我是中国人00");
String allPinYin2 = getAllPinYin("王宇");
System.out.println(allPinYin);
System.out.println(allPinYin2);
System.out.println(firstPinYin);
}
/** * Get the full pinyin of a Chinese character string * @param hanZi '我是中国人00' * @return */
public static String getAllPinYin(String hanZi){
//This class defines how to output Hanyu Pinyin.
HanyuPinyinOutputFormat pinyinOutputFormat = new HanyuPinyinOutputFormat();
pinyinOutputFormat.setCaseType(HanyuPinyinCaseType.LOWERCASE);//Output in lowercase,LOWERCASE:输出小写,UPPERCASE:输出大写
pinyinOutputFormat.setToneType(HanyuPinyinToneType.WITH_TONE_MARK);//Output phonetic settings,WITH_TONE_MARK:Use phonetic symbols directly(必须设置WITH_U_UNICODE,否则会抛出异常),WITH_TONE_NUMBER:1-4Numbers represent phonetic symbols,WITHOUT_TONE:没有音标
pinyinOutputFormat.setVCharType(HanyuPinyinVCharType.WITH_U_UNICODE);//special phonetic symbolsü的设置,(WITH_V:用v表示ü,WITH_U_AND_COLON:用"u:"表示ü,WITH_U_UNICODE:直接用ü)
char[] hanziChars = hanZi.trim().toCharArray();//汉字数组,去掉前后多余的空格
StringBuilder stringBuilder = new StringBuilder();
try {
for (int i = 0; i < hanziChars.length; i++) {
String hanziString = Character.toString(hanziChars[i]);//Get a single Chinese character,例如‘中’
if(hanziString.matches("[\\u4e00-\\u9fa5]")){
//判断是否是汉字
String[] pinYinAll = PinyinHelper.toHanyuPinyinStringArray(hanziChars[i],pinyinOutputFormat);//Get all the pronunciations of this Chinese character,For example, Zhong is a polyphonic word,其结果就是['zhōng','zhòng']
String oneHanZiPinYin = pinYinAll[0];//Directly take a pinyin as a Chinese character,例如zhōng
stringBuilder.append(oneHanZiPinYin);
}else {
//Chinese characters are not output as they are
stringBuilder.append(hanziString);
}
}
} catch (BadHanyuPinyinOutputFormatCombination badHanyuPinyinOutputFormatCombination) {
badHanyuPinyinOutputFormatCombination.printStackTrace();
}
String allPinYin = stringBuilder.toString();//Returns the full spelling of Chinese strings,例如:wŏshìzhōngzhòngguórén00
return allPinYin;
}
/** * Get the pinyin of the first letter of a Chinese character string * @param hanZi '我是中国人00' * @return */
public static String getFirstPinYin(String hanZi){
//This class defines how to output Hanyu Pinyin.
HanyuPinyinOutputFormat pinyinOutputFormat = new HanyuPinyinOutputFormat();
pinyinOutputFormat.setCaseType(HanyuPinyinCaseType.LOWERCASE);//Output in lowercase,LOWERCASE:输出小写,UPPERCASE:输出大写
pinyinOutputFormat.setToneType(HanyuPinyinToneType.WITH_TONE_MARK);//Output phonetic settings,WITH_TONE_MARK:Use phonetic symbols directly(必须设置WITH_U_UNICODE,否则会抛出异常),WITH_TONE_NUMBER:1-4Numbers represent phonetic symbols,WITHOUT_TONE:没有音标
pinyinOutputFormat.setVCharType(HanyuPinyinVCharType.WITH_U_UNICODE);//special phonetic symbolsü的设置,(WITH_V:用v表示ü,WITH_U_AND_COLON:用"u:"表示ü,WITH_U_UNICODE:直接用ü)
char[] hanziChars = hanZi.trim().toCharArray();//去掉前后多余的空格,Convert to an array of Chinese characters
StringBuilder stringBuilder = new StringBuilder();
try {
for (int i = 0; i < hanziChars.length; i++) {
String hanziString = Character.toString(hanziChars[i]);//Get a single Chinese character,例如‘中’
if(hanziString.matches("[\\u4e00-\\u9fa5]")){
//判断是否是汉字
String[] pinYinAll = PinyinHelper.toHanyuPinyinStringArray(hanziChars[i],pinyinOutputFormat);//Get all the pronunciations of this Chinese character,For example, Zhong is a polyphonic word,其结果就是['zhōng','zhòng']
String firstString = Character.toString(pinYinAll[0].charAt(0));//The first character to go to the first pinyin is the first letter of Chinese characters
stringBuilder.append(firstString);
}else {
//Chinese characters are not output as they are
stringBuilder.append(hanziString);
}
}
} catch (BadHanyuPinyinOutputFormatCombination badHanyuPinyinOutputFormatCombination) {
badHanyuPinyinOutputFormatCombination.printStackTrace();
}
String allPinYin = stringBuilder.toString();//Returns the initial pinyin of a Chinese string
return allPinYin;
}
}
输出结果
wŏshìzhōngguórén00
wángyŭ
wszgr00
边栏推荐
- Quickly learn chess from zero to one
- HOG feature study notes
- 【genius_platform软件平台开发】第七十六讲:vs预处理器定义的牛逼写法!!!!(其他组牛逼conding人员告知这么配置来取消宏定义)
- mysql tree structure query problem
- How do programmers without objects spend the Chinese Valentine's Day
- js中try...catch和finally的用法
- 1527. 患某种疾病的患者
- 采用redis缓存的linux主从同步服务器图片硬盘满了移到新目录要修改哪些指向
- [LeetCode Brush Questions] - Sum of Numbers topic (more topics to be added)
- OpenGL 工作原理
猜你喜欢

常见的硬件延迟

云原生(三十二) | Kubernetes篇之平台存储系统介绍

How do programmers without objects spend the Chinese Valentine's Day

The 20th day of the special assault version of the sword offer

【MySQL series】- Does LIKE query start with % will make the index invalid?

js中try...catch和finally的用法

继承关系下构造方法的访问特点

select tag custom style

【解密】OpenSea免费创造的NFT都没上链竟能出现在我的钱包里?

.Net C# Console Create a window using Win32 API
随机推荐
重新审视分布式系统:永远不会有完美的一致性方案……
行业案例|世界 500 强险企如何建设指标驱动的经营分析系统
The 2022 EdgeX China Challenge will be grandly opened on August 3
用@Mapper查询oracle的分区情况报错
STM32使用stm32cubemx LL库系列教程
Greenplum Database Fault Analysis - Can a Soft Connection Be Made to the Database Base Folder?
View handler 踩坑记录
协作D2D局部模型聚合的半分散联合学习
海量服务实例动态化管理
C学生管理系统 指定位置插入学生节点
select tag custom style
虚拟内存原理与技术
Note that Weifang generally needs to pay attention to issuing invoices
leetcode 15
VSCode Change Default Terminal 如何修改vscode的默认terminal
Regular expression to match a certain string in the middle
Pisanix v0.2.0 发布|新增动态读写分离支持
Tree search (bintree)
特殊矩阵的压缩存储
.Net C# Console Create a window using Win32 API