当前位置:网站首页>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
边栏推荐
猜你喜欢
随机推荐
Dotnet 6 Why does the network request not follow the change of the system network proxy and dynamically switch the proxy?
iNFTnews | What can NFTs bring to the sports industry and fans?
回顾51单片机
C语言日记 9 if的3种语句
Optimizing the feed flow encountered obstacles, who helped Baidu break the "memory wall"?
【LeetCode刷题】-数之和专题(待补充更多题目)
View handler stepping record
C language implements a simple number guessing game
采用redis缓存的linux主从同步服务器图片硬盘满了移到新目录要修改哪些指向
SuperMap iDesktop.Net之布尔运算求交——修复含拓扑错误复杂模型
lua learning
汉字转拼音
【解密】OpenSea免费创造的NFT都没上链竟能出现在我的钱包里?
C学生管理系统 据学号查找学生节点
How to deal with your own shame
基于左序遍历的数据存储实践
[In-depth study of 4G/5G/6G topic-51]: URLLC-16-"3GPP URLLC related protocols, specifications, and technical principles in-depth interpretation"-11-High reliability technology-2-Link adaptive enhancem
ARM Mailbox
用@Mapper查询oracle的分区情况报错
QT MV\MVC结构