前言
在实际开发中,可能会遇到根据用户输入的中文拼音首字母缩写,来查询对应数据的功能需求,本文将提供一个操作中文转拼音、获取拼音首字母的工具类CV即用。
一、PinYin4j 简介 :
pinyin4j是一个开源的流行java库(官网,在线文档 ),使用来处理中文转换成拼音(汉语拼音,罗马拼音等),功能强大。
有几个常用的类:
net.sourceforge.pinyin4j.PinyinHelper;
net.sourceforge.pinyin4j.format.HanyuPinyinCaseType;
net.sourceforge.pinyin4j.format.HanyuPinyinOutputFormat;
net.sourceforge.pinyin4j.format.HanyuPinyinToneType;
net.sourceforge.pinyin4j.format.HanyuPinyinVCharType;
PinyinHelper:提供了几个实用程序函数,用于将中文字符(简体和繁体)转换为各种中文罗马化表示。
HanyuPinyinOutputFormat:这个类定义了如何输出汉语拼音。
HanyuPinyinCaseType:为汉语拼音字符串的输出案例提供了几种选项。
HanyuPinyinToneType:该类提供了几种输出中文音调的选项。
HanyuPinyinVCharType:这个类为'ü'的输出提供了几个选项。
Pinyin4j支持多种格式:
- 全部大小YYH
- 全部大写(中间加字符串*)
- 全部小写
- 全部小写(中间加字符串*)
- 返回首字母大写H
- 返回首字母小写h
二、PinYin4j的工具类 :
- 首先我们需要引入PinYin4j的依赖
<!--中文转拼音-->
<dependency>
<groupId>com.belerweb</groupId>
<artifactId>pinyin4j</artifactId>
<version>2.5.0</version>
</dependency>
- 工具类
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.HanyuPinyinVCharType;
import net.sourceforge.pinyin4j.format.exception.BadHanyuPinyinOutputFormatCombination;
/**
* @author 码不多
* @version 1.0
* @description: 此工具类为中文 -> 拼音的操作工具类
* @date 2021/9/1 10:53
*/
public class PinYinUtil {
/**
* 功能描述: 中文转拼音
* @author 码不多
* @date 2021/9/1
* @param str
* @return java.lang.String
*/
public static String getPinYin(String str){
//创建空字符数组报错参数字符串转为字符后的数据
char[] t1 = null;
//将字符串转为字符数组
t1 = str.toCharArray();
//创建字符串数组,将长度定位字符数组的长度
String[] t2 = new String[t1.length];
//创建汉语拼音处理类对象
HanyuPinyinOutputFormat t3 = new HanyuPinyinOutputFormat();
/**
* 设置输出类型
* UPPERCASE:大写 (ZHONG)
* LOWERCASE:小写 (zhong)
* WITHOUT_TONE:无音标 (zhong)
* WITH_TONE_NUMBER:1-4数字表示英标 (zhong4)
* WITH_TONE_MARK:直接用音标符(必须WITH_U_UNICODE否则异常) (zhòng)
* WITH_V:用v表示ü (nv)
* WITH_U_AND_COLON:用"u:"表示ü (nu:)
* WITH_U_UNICODE:直接用ü (nü)
*/
t3.setCaseType(HanyuPinyinCaseType.LOWERCASE);
t3.setToneType(HanyuPinyinToneType.WITHOUT_TONE);
t3.setVCharType(HanyuPinyinVCharType.WITH_V);
//创建空字符串
String t4 = "";
//保存字符数组的长度
int t0 = t1.length;
try {
//遍历
for (int i = 0; i < t0; i++) {
//判断是否为汉字字符
if (Character.toString(t1[i]).matches("[\\u4E00-\\u9FA5]+")) {
/**
*传入字符,如果传入的字符不是汉字不能转换成拼音,那么会直接返回null
*/
//获取转换为拼音后的数据,如果传入的字符不是汉字不能转换成拼音,那么会直接返回null
t2 = PinyinHelper.toHanyuPinyinStringArray(t1[i], t3);
//将其取出转换成字符串
t4 += t2[0];
} else {
//如果不是字符串,直接将结果原样返回
t4 += Character.toString(t1[i]);
}
}
//返回结果
return t4;
} catch (BadHanyuPinyinOutputFormatCombination e1)
{
e1.printStackTrace();
}
//返回结果
return t4;
}
/**
* 功能描述: 中文转拼音并获取首字母缩写
* @author 码不多
* @date 2021/9/1
* @param str
* @return java.lang.String
*/
public static String getPinYinHeadChar(String str){
//定义空字符串,用来存储最终的拼音缩写首字母组合
String convert = "";
//遍历
for (int j = 0; j < str.length(); j++) {
//返回遍历索引出的单字符
char word = str.charAt(j);
//创建字符串数组,传入字符,如果传入的字符不是汉字不能转换成拼音,那么会直接返回null
String[] pinyinArray = PinyinHelper.toHanyuPinyinStringArray(word);
//判断存储拼音的字符串数组不为null
if (pinyinArray != null) {
//将其取出,并获取第一个首字母并拼接
convert += pinyinArray[0].charAt(0);
} else {
//原样输出
convert += word;
}
}
//将结果返回
return convert;
}
}
总结 : 这个工具类暂时只写了两个功能,后期会补加功能。使用只需要将你需要转换的中文字符串作为参数传递进去就可以了。