当前位置:网站首页>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
边栏推荐
- LPQ (local phase quantization) study notes
- 回顾51单片机
- 【解密】OpenSea免费创造的NFT都没上链竟能出现在我的钱包里?
- 线上MySQL的自增id用尽怎么办?
- 【MySQL series】- Does LIKE query start with % will make the index invalid?
- Access Characteristics of Constructor under Inheritance Relationship
- Hypervisor related knowledge points
- matlab绘制用颜色表示模值大小的箭头图
- LeetCode使用最小花费爬楼梯----dp问题
- Gantt chart is here, project management artifact, template is used directly
猜你喜欢

如何逐步执行数据风险评估

C language implements a simple number guessing game

【genius_platform软件平台开发】第七十六讲:vs预处理器定义的牛逼写法!!!!(其他组牛逼conding人员告知这么配置来取消宏定义)

What should I do if the self-incrementing id of online MySQL is exhausted?
![02 [Development Server Resource Module]](/img/60/f77ed0bb0e5654c9dcd70b73a5bee8.png)
02 [Development Server Resource Module]

甘特图来啦,项目管理神器,模板直接用
![01 [Foreword Basic Use Core Concepts]](/img/90/67537d5fad28d68766ca85b887839e.png)
01 [Foreword Basic Use Core Concepts]

Intel XDC 2022 Wonderful Review: Build an Open Ecosystem and Unleash the Potential of "Infrastructure"

sql语句多字段多个值如何进行排序

在这个超连接的世界里,你的数据安全吗
随机推荐
剑指offer专项突击版第20天
C学生管理系统 指定位置插入学生节点
虚拟内存原理与技术
【存储】曙光存储DS800-G35 ISCSI各映射LUN给服务器
[深入研究4G/5G/6G专题-51]: URLLC-16-《3GPP URLLC相关协议、规范、技术原理深度解读》-11-高可靠性技术-2-链路自适应增强(根据无线链路状态动态选择高可靠性MCS)
How do programmers without objects spend the Chinese Valentine's Day
sql语句多字段多个值如何进行排序
Simple implementation of YOLOv7 pre-training model deployment based on OpenVINO toolkit
Programmer's list of sheep counting when insomnia | Daily anecdote
【 2 】 OpenCV image processing: basic knowledge of OpenCV
2022了你还不会『低代码』?数据科学也能玩转Low-Code啦!
QT MV\MVC结构
程序员的七夕浪漫时刻
Domain Driven Design - MDD
SDC简介
Amazon Cloud Technology joins hands with Thundersoft to build an AIoT platform for industry customers
[Decryption] Can the NFTs created by OpenSea for free appear in my wallet without being chained?
QT:神奇QVarient
Dotnet 6 Why does the network request not follow the change of the system network proxy and dynamically switch the proxy?
Tree search (bintree)