java get the first letter of characters

 Import jar package:

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

Code: 

  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 is      / ** 
. 19       * Get characters spelling the first letters or capital letters
 20 is       *
 21 is       * @param chinese characters
 22 is       * @param whether isFull spelling to true: false indicates spelling represents: initials
 23 is       *
 24       * @return spelling or characters capitalized channeling
 25       * / 
26 is      public  static String getUppercase (chinese String, Boolean isFull) {
 27          return convertHanzi2Pinyin (chinese, isFull) .toUpperCase ();
 28      }
 29  
30      / ** 
31 is      * Get characters spelling the first letters or lower-case letters
 32       *
 33 is       * @param chinese characters
 34 is       * @param isFull whether spelling to true: false indicates spelling represents: initials
 35       *
 36       * @return spelling first letter or lowercase characters channeling
 37 [       * / 
38 is      public  static   String getLowercase (chinese String, Boolean isFull) {
 39          return convertHanzi2Pinyin (chinese, isFull) .toLowerCase ();
 40      }
 41 is  
42 is      / ** 
43 is       * converted into phonetic chinese characters
 44 is       * <P>
 45      * Acronym or spelling
 46 is       *
 47       * @param Hanzi characters string
 48       * @param isFull whether spelling to true: false indicates spelling represents: initials
 49       *
 50       * @return history
 51 is       * / 
52 is      Private  static String convertHanzi2Pinyin (String Hanzi, Boolean isFull) {
 53 is          / ** *
 54 is           * ^ [\ u2E80- \ u9FFF] + $ matches all languages of East Asia
 55           * ^ [\ u4E00- \ u9FFF] + $ matches simplified and traditional
 56           * ^ [\ u4E00- \ u9FA5] + $ matches Simplified
 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 is                  }
 72                  the else {
 73 is                      sb.append (pinyin.charAt (0 ));
 74                  }
 75              } the else {
 76                  sb.append (Unit) ;
 77              }
 78          }
 79          return sb.toString ();
 80      }
 81  
82      / ** 
83       * converted into single pinyin characters
 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, the outputFormat);
 96              sb.append (RES [0]); // for polyphone, only the first alphabet 
97          } the catch (Exception E) {
 98              e.printStackTrace ();
 99              return " " ;
 100          }
 101          return sb.toString ();
 102      }
 103  
104      / ** *
 105       * match
 106       * <P>
 107       * the character matching and regular expressions
 108       *
 109       * @param STR source string
 110      * @Param REGEX regular expression
 111       *
 112       * @return to true: match succeeds false: the matching fails
 113       * / 
114      Private  static  Boolean match (STR String, String REGEX) {
 115          the Pattern = pattern of Pattern.compile (REGEX);
 1 16          Matcher = Matcher Pattern.matcher (STR);
 117          return matcher.find ();
 1 18      }
 119  
120      / ** 
121       * test method
 122       * / 
123      public  static  void main (String [] args) {
124          System.out.println (convertHanzi2Pinyin ( "Ferguson Guangdong Province Q", to false ) .toLowerCase ());
 125      }
 126 }

 

Guess you like

Origin www.cnblogs.com/chLxq/p/11839677.html