使用pinyin4j获取汉字首字母

本文介绍了Pinyin4J库的使用方法,该库能够实现汉字到拼音的转换,并提供了多个拼音系统的转换选项。文中详细解释了核心方法toHanyuPinyinStringArray的功能及参数配置,展示了如何设置拼音输出格式。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

pinyin4J 是一个可以将汉字转换成拼音的lib,非常实用,其maven地址为:http://mvnrepository.com/artifact/com.belerweb/pinyin4j/2.5.0
pinyin4J 提供PinyinHelper这个静态类对外提供拼音转换的服务,主要有一下方法:

[java] view plain copy
static public String[] toHanyuPinyinStringArray(char ch)
将char(必须为汉字单字)转化为拼音,实用的是通用的格式,如果ch为非汉字,返回null。
输入:重 输出:[zhong4, chong2]
说明重字有两个读音,拼音后面的1,2,3,4 代表的是读音
[java] view plain copy
static public String[] toHanyuPinyinStringArray(char ch,HanyuPinyinOutputFormat outputFormat)
同上,但是这个方法可以设置输出的格式。HanyuPinyinOutputFormat 可以设置拼音大小写、是否后面加读音数字、特殊读音的显示方式,定义如下:
[java] view plain copy


/** 
 * The option indicates that the output of 'ü' is "u:" 
 */  
public static final HanyuPinyinVCharType WITH_U_AND_COLON = new HanyuPinyinVCharType("WITH_U_AND_COLON");  
/** 
 * The option indicates that the output of 'ü' is "v" 
 */  
public static final HanyuPinyinVCharType WITH_V = new HanyuPinyinVCharType("WITH_V")  
/** 
 * The option indicates that the output of 'ü' is "ü" in Unicode form 
 */  
public static final HanyuPinyinVCharType WITH_U_UNICODE = new HanyuPinyinVCharType("WITH_U_UNICODE");  
[java] view plain copy
static public String[] toTongyongPinyinStringArray(char ch)     //转换为通用拼音  
static public String[] toWadeGilesPinyinStringArray(char ch)    //转换为威妥玛拼音  
static public String[] toMPS2PinyinStringArray(char ch)         //转换为注音符号拼音  
static public String[] toYalePinyinStringArray(char ch)         //转换为耶魯拼音  
static public String[] toGwoyeuRomatzyhStringArray(char ch)     //转换为国语罗马字  

实例代码:

 List<Map<String,String>> merchantList = appOrderService.queryMerchantList();
        TreeMap<String,List<Map<String,String>>> result=merchantList.stream().collect(Collectors.groupingBy((map) -> {
            String merchantName = map.getOrDefault("merchant_name", "#");
            String regex = "[\u4e00-\u9fa5]+";
            //判断第一位字符是否为中文
            String substring = merchantName.substring(0, 1);
            if(substring.matches(regex)){
                //中文
                char cha = merchantName.charAt(0);
                HanyuPinyinOutputFormat hanyuPinyinOutputFormat = new HanyuPinyinOutputFormat();
                //全部大写
                hanyuPinyinOutputFormat.setCaseType(HanyuPinyinCaseType.UPPERCASE);
                //'ü' 转换为 "v"
                hanyuPinyinOutputFormat.setVCharType(HanyuPinyinVCharType.WITH_V);
                //去除数字或音色
                hanyuPinyinOutputFormat.setToneType(HanyuPinyinToneType.WITHOUT_TONE);
                String[] pinyinArray=null;
                try {
                    pinyinArray =  PinyinHelper.toHanyuPinyinStringArray(cha,hanyuPinyinOutputFormat );
                } catch (BadHanyuPinyinOutputFormatCombination badHanyuPinyinOutputFormatCombination) {
                    badHanyuPinyinOutputFormatCombination.printStackTrace();
                }
                return ArrayUtils.isEmpty(pinyinArray)?"#":pinyinArray[0].substring(0,1);
            }else if(StringUtils.isAlpha(substring)){
                //为字母
                return substring.toUpperCase();
            }else{
                //非中文非数字
                return "#";
            }
        }, TreeMap::new, Collectors.toList()));
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值