当前位置:网站首页>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
边栏推荐
- 树表的查找
- SDC简介
- leetcode-另一棵树的子树
- nodeJs--封装路由
- 用@Mapper查询oracle的分区情况报错
- Optimizing the feed flow encountered obstacles, who helped Baidu break the "memory wall"?
- 【解密】OpenSea免费创造的NFT都没上链竟能出现在我的钱包里?
- Note that Weifang generally needs to pay attention to issuing invoices
- Pisanix v0.2.0 released | Added support for dynamic read-write separation
- LeetCode使用最小花费爬楼梯----dp问题
猜你喜欢
随机推荐
Optimizing the feed flow encountered obstacles, who helped Baidu break the "memory wall"?
Go 微服务开发框架 DMicro 的设计思路
【genius_platform软件平台开发】第七十六讲:vs预处理器定义的牛逼写法!!!!(其他组牛逼conding人员告知这么配置来取消宏定义)
Domain Driven Design - MDD
C学生管理系统 指定位置插入学生节点
QStyle平台风格
Advanced Numbers_Review_Chapter 1: Functions, Limits, Continuity
mysql tree structure query problem
DAY22:sqli-labs 靶场通关wp(Less01~~Less20)
js中try...catch和finally的用法
DAY22: sqli-labs shooting range clearance wp (Less01~~Less20)
Note that Weifang generally needs to pay attention to issuing invoices
[机缘参悟-60]:《兵者,诡道也》-2-孙子兵法解读
【C语言】详解栈和队列(定义、销毁、数据的操作)
注意潍坊开具发票一般需要注意
LeetCode uses the minimum cost to climb the stairs----dp problem
How to deal with your own shame
post-study program
基于左序遍历的数据存储实践
VSCode Change Default Terminal 如何修改vscode的默认terminal








