java汉字获取首字母

 导入jar包:

1 <dependency>
2   <groupId>com.belerweb</groupId>
3   <artifactId>pinyin4j</artifactId>
4   <version>2.5.0</version>
5 </dependency>

代码: 

  1 package org.jeecg.modules.system.util;
  2 
  3 import net.sourceforge.pinyin4j.PinyinHelper;
  4 import net.sourceforge.pinyin4j.format.HanyuPinyinOutputFormat;
  5 import net.sourceforge.pinyin4j.format.HanyuPinyinToneType;
  6 
  7 import java.util.regex.Matcher;
  8 import java.util.regex.Pattern;
  9 
 10 /**
 11  * 获取首字母工具
 12  *
 13  * @author
 14  * @Date
 15  */
 16 public class ChineseCharacterUtil {
 17 
 18     /**
 19      *  获取汉字首字母或全拼大写字母
 20      *
 21      * @param chinese 汉字
 22      * @param isFull  是否全拼 true:表示全拼 false表示:首字母
 23      *
 24      * @return 全拼或者首字母大写字符窜
 25      */
 26     public static String getUpperCase(String chinese,boolean isFull){
 27         return convertHanzi2Pinyin(chinese,isFull).toUpperCase();
 28     }
 29 
 30     /**
 31      * 获取汉字首字母或全拼小写字母
 32      *
 33      * @param chinese 汉字
 34      * @param isFull 是否全拼 true:表示全拼 false表示:首字母
 35      *
 36      * @return 全拼或者首字母小写字符窜
 37      */
 38     public static  String getLowerCase(String chinese,boolean isFull){
 39         return convertHanzi2Pinyin(chinese,isFull).toLowerCase();
 40     }
 41 
 42     /**
 43      * 将汉字转成拼音
 44      * <P>
 45      * 取首字母或全拼
 46      *
 47      * @param hanzi 汉字字符串
 48      * @param isFull 是否全拼 true:表示全拼 false表示:首字母
 49      *
 50      * @return 拼音
 51      */
 52     private static String convertHanzi2Pinyin(String hanzi,boolean isFull){
 53         /***
 54          * ^[\u2E80-\u9FFF]+$ 匹配所有东亚区的语言
 55          * ^[\u4E00-\u9FFF]+$ 匹配简体和繁体
 56          * ^[\u4E00-\u9FA5]+$ 匹配简体
 57          */
 58         String regExp="^[\u4E00-\u9FFF]+$";
 59         StringBuffer sb=new StringBuffer();
 60         if(hanzi==null||"".equals(hanzi.trim())){
 61             return "";
 62         }
 63         String pinyin="";
 64         for(int i=0;i<hanzi.length();i++){
 65             char unit=hanzi.charAt(i);
 66             //是汉字,则转拼音
 67             if(match(String.valueOf(unit),regExp)){
 68                 pinyin=convertSingleHanzi2Pinyin(unit);
 69                 if(isFull){
 70                     sb.append(pinyin);
 71                 }
 72                 else{
 73                     sb.append(pinyin.charAt(0));
 74                 }
 75             }else{
 76                 sb.append(unit);
 77             }
 78         }
 79         return sb.toString();
 80     }
 81 
 82     /**
 83      * 将单个汉字转成拼音
 84      *
 85      * @param hanzi 汉字字符
 86      *
 87      * @return 拼音
 88      */
 89     private static String convertSingleHanzi2Pinyin(char hanzi){
 90         HanyuPinyinOutputFormat outputFormat = new HanyuPinyinOutputFormat();
 91         outputFormat.setToneType(HanyuPinyinToneType.WITHOUT_TONE);
 92         String[] res;
 93         StringBuffer sb=new StringBuffer();
 94         try {
 95             res = PinyinHelper.toHanyuPinyinStringArray(hanzi,outputFormat);
 96             sb.append(res[0]);//对于多音字,只用第一个拼音
 97         } catch (Exception e) {
 98             e.printStackTrace();
 99             return "";
100         }
101         return sb.toString();
102     }
103 
104     /***
105      * 匹配
106      * <P>
107      * 根据字符和正则表达式进行匹配
108      *
109      * @param str 源字符串
110      * @param regex 正则表达式
111      *
112      * @return true:匹配成功  false:匹配失败
113      */
114     private static boolean match(String str,String regex){
115         Pattern pattern= Pattern.compile(regex);
116         Matcher matcher=pattern.matcher(str);
117         return matcher.find();
118     }
119 
120     /**
121      * 测试方法
122      */
123     public static void main(String[] args) {
124         System.out.println(convertHanzi2Pinyin("弗格森的广东省q",false).toLowerCase());
125     }
126 }

猜你喜欢

转载自www.cnblogs.com/chLxq/p/11839677.html