1、pom中引入包
<dependency> <groupId>com.belerweb</groupId> <artifactId>pinyin4j</artifactId> <version>2.5.0</version> </dependency>
2、代码
package com.example.demo.utils; 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 java.util.regex.Matcher; import java.util.regex.Pattern; /** * 汉字工具类 */ public class ChineseCharacterUtil { /*** * 将汉字转成拼音(取首字母或全拼) * @param hanzi * @param full 是否全拼 true-全拼 false-首字母 * 如哈尔滨 haerbin heb * @return */ public static String convertHanzi2Pinyin(String hanzi,boolean full) { /*** * ^[\u2E80-\u9FFF]+$ 匹配所有东亚区的语言 * ^[\u4E00-\u9FFF]+$ 匹配简体和繁体 * ^[\u4E00-\u9FA5]+$ 匹配简体 */ String regExp="^[\u4E00-\u9FFF]+$"; StringBuffer sb=new StringBuffer(); if(hanzi==null||"".equals(hanzi.trim())) { return ""; } String pinyin=""; for(int i=0;i<hanzi.length();i++) { char unit=hanzi.charAt(i); if(match(String.valueOf(unit),regExp))//是汉字,则转拼音 { pinyin=convertSingleHanzi2Pinyin(unit,hanzi); if(full) { sb.append(pinyin); } else { sb.append(pinyin.charAt(0)); } } else { sb.append(unit); } } return sb.toString(); } /*** * @param str 源字符串 * @param regex 正则表达式 * @return 是否匹配 */ public static boolean match(String str,String regex) { Pattern pattern=Pattern.compile(regex); Matcher matcher=pattern.matcher(str); return matcher.find(); } /*** * 将单个汉字转成拼音 * @param hanzi * 汉字有可能是多音字 * @return */ private static String convertSingleHanzi2Pinyin(char hanzi,String hanziAll) { HanyuPinyinOutputFormat outputFormat = new HanyuPinyinOutputFormat(); // 设置声调格式 /** * HanyuPinyinToneType.WITH_TONE_NUMBER 用数字表示声调,例如:liu2 HanyuPinyinToneType.WITHOUT_TONE 无声调表示,例如:liu HanyuPinyinToneType.WITH_TONE_MARK 用声调符号表示,例如:liú */ outputFormat.setToneType(HanyuPinyinToneType.WITHOUT_TONE);//无声调 // 设置大小写 outputFormat.setCaseType(HanyuPinyinCaseType.LOWERCASE); // 设置特殊拼音的显示格式 /** * HanyuPinyinVCharType.WITH_U_AND_COLON 以U和一个冒号表示该拼音,例如:lu: HanyuPinyinVCharType.WITH_V 以V表示该字符,例如:lv HanyuPinyinVCharType.WITH_U_UNICODE 以ü表示 */ String[] res; try { res = PinyinHelper.toHanyuPinyinStringArray(hanzi,outputFormat); return pinyinMor(res,hanziAll); //sb.append(res[0]);//对于多音字,只用第一个拼音 } catch (Exception e) { e.printStackTrace(); } return ""; } /** * 是否是多音字 * res 拼音数据组 * hanziAll 汉字 */ private static String pinyinMor(String[] res,String hanziAll){ //是多音字 if(res.length>1){ if(hanziAll.indexOf("市")>-1){ return res[1]; } } //不是多音字返回 return res[0]; } public static void main(String[] args) { String cityName="重庆市"; //全拼 System.out.println(cityName+":"+"全拼->"+convertHanzi2Pinyin(cityName,true)); //首字母 System.out.println(cityName+":"+"首字母->"+convertHanzi2Pinyin(cityName,false)); } }
3、结果
判断是否是多音字后输出结果
重庆市:全拼->chongqingshi
重庆市:首字母->cqs
不判断是否是多音字后输出结果
zhong
chong
qing
shi
重庆市:全拼->zhongqingshi
zhong
chong
qing
shi
重庆市:首字母->zqs