병음 도구 JAVA

프로젝트에 항아리 패키지를 사용하기 전에 가져 오기 pinyin4j :

< 의존성 > 
          < 의 groupId > com.belerweb </ 의 groupId > 
          < artifactId를 > pinyin4j </ artifactId를 > 
          < 버전 > 2.5.0 </ 버전 > 
</ 의존성 >

여기, 포장 도구 :

패키지 유틸; 

수입 net.sourceforge.pinyin4j.PinyinHelper;
수입 net.sourceforge.pinyin4j.format.HanyuPinyinCaseType;
수입 net.sourceforge.pinyin4j.format.HanyuPinyinOutputFormat;
수입 net.sourceforge.pinyin4j.format.HanyuPinyinToneType;
수입 net.sourceforge.pinyin4j.format.exception.BadHanyuPinyinOutputFormatCombination; 

/ ** 
 * 
 * 2014년 12월 4일에 zengxm 작성. 
 * / 
공용  클래스 PinyinUtils { 
    HanyuPinyinOutputFormat 형식 = ;
    공공  정적  열거  형 {
        대문자,               // 모두 대문자 
        소문자               // 모두 소문자 
        FIRSTUPPER,               // 이니셜 
        ONLYFIRSTCHAR     // 첫 번째 편지 - 자본 
    }
     정적 PinyinUtils = 기록 새로운 새로운 PinyinUtils ();
     공공 PinyinUtils () { 
        형식 = 새로운 새로운 HanyuPinyinOutputFormat을 (); 
        format.setCaseType (HanyuPinyinCaseType.UPPERCASE) 
        format.setToneType (HanyuPinyinToneType.WITHOUT_TONE는); 
    } 
    // --Ming 내일 티안를 대문자로 
    공공  정적을문자열 toPinYinFirstUpper (문자열 str을, 문자열 SPERA) { 
        문자열 병음; 
        시도 { 
            병음 = record.toPinYin (캐릭터, SPERA, Type.FIRSTUPPER);
            경우 ( "0123456789" .contains (병음)) { 
                병음 = numBerToPinyin (병음); 
            } 
            반환 병음을; 
        } 캐치 (BadHanyuPinyinOutputFormatCombination 전자) {
             반환 "" ; 
        } 
    } 
    // 全部大写明天--MINGTIAN 
    공공  정적 문자열 toPinYin (문자열 str을, 문자열 SPERA) {
        문자열 병음; 
        시도 { 
            병음 =record.toPinYin (STR, SPERA, Type.UPPERCASE);
            반환 병음을; 
        } 캐치 (BadHanyuPinyinOutputFormatCombination 전자) {
             반환 "" ; 
        } 
    } 
    // 只包含首字母,大写明天--MT 
    공공  정적 문자열 OnlyFirstChar_Upper (문자열 STR) { 
        문자열 병음; 
        시도 { 
            병음 = record.toPinYin (STR, "" , Type.FIRSTUPPER);
            경우 (pinYin.length () == 0 ) {
                 반환 "" ; 
            } 
            문자열 [] 배열PinYin.split = ( "" ) 
            문자열 FirstChar = "" ;
             (문자열 STE : 배열) {
                 IF (ste.length ()> 0 ) { 
                    FirstChar   + = ste.substring (0,1 ) .toUpperCase (); 
                } 
            } 
            반환 ; FirstChar을 
        } 캐치 (BadHanyuPinyinOutputFormatCombination E) {
             반환 "" ; 
        } 
    } 
    
    / ** 
     * str을 음성으로 변환, 또는 해당하는 한자를 잘 작성하지 변환, 알파벳하지 않는 경우 
     로 * : MINGTIAN의로 변환 내일 
     *의파라미터 : STR의 
     *의 {파라미터 : SPERA는 
     * @return 
     * @throws BadHanyuPinyinOutputFormatCombination는
      /를 * 
    공공   문자열 toPinYin은 (문자열 str을, 문자열 SPERA, 유형 유형) 가 발생 BadHanyuPinyinOutputFormatCombination {
         경우 (캐릭터 == null의 || str.trim (). 길이 () == 0 ) {
             반환 "" ; 
        } 
        경우 (유형 == Type.UPPERCASE) { 
            format.setCaseType (HanyuPinyinCaseType.UPPERCASE); 
        } 다른
        }
            format.setCaseType (HanyuPinyinCaseType.LOWERCASE); 
        문자열 평 = "" ; 
        문자열 온도 = "" ; 
        문자열 [] t; 
        위한 ( int로 I = 0; I)이 (str.length를 <; 내가 ++ ) {
             CHAR C = str.charAt (I);
            경우 (( INT ) C <= 128 ) { 
                PY + = C; 
            } 다른 { 
                t = PinyinHelper.toHanyuPinyinStringArray (c 포맷);
                만약 (t에서의 == ) { 
                    PY+ = C; 
                }{ 
                    임시 =를 t [0 ];
                    경우 (유형 == Type.FIRSTUPPER) { 
                        임시 =를 t [0] .toUpperCase () 수 charAt (0) + temp.substring (1. ); 
                    } 
                    PY + = + 온도 (I str.length == () - 1 ","? : SPERA); 
                } 
            } 
        } 
        
        리턴 ) (py.trim; 
    } 
    
    공용  정적 스트링 getFirstSpell (중국어 문자열) { 
        StringBuffer를 pybf = 의 StringBuffer (); 
         [] = 도착 chinese.toCharArray (); 
        HanyuPinyinOutputFormat defaultFormat = 새로운 HanyuPinyinOutputFormat (); 
        defaultFormat.setCaseType (HanyuPinyinCaseType.LOWERCASE); 
        defaultFormat.setToneType (HanyuPinyinToneType.WITHOUT_TONE); 
        위한 ( int로 I = 0; I <arr.length; 내가 ++ ) { 
                 경우 ([I]가 도착> 128 ) { 
                         시도 { 
                                문자열 [] 온도 = PinyinHelper.toHanyuPinyinStringArray (도착 [I] defaultFormat); 
                                경우 (온도! = null이 ) {
                                        pybf.append (TEMP [ 0] .charAt (0 )); 
                                } 
                        } 캐치 (BadHanyuPinyinOutputFormatCombination 전자) { 
                                e.printStackTrace (); 
                        } 
                } { 
                        pybf.append (도착 [I]); 
                } 
        } 
        리턴 . ( "", "\\ W"완전히 대체 pybf.toString () ) () .trim; 
    } 

    // 数字转换成对应拼音
    공공  정적 문자열 numBerToPinyin (문자열 STR) { 
        문자열 병음 =널 (null) ;
        스위치 (STR) {
         경우 "0" : 
            병음 = "Z" ;
            휴식 ;
        경우 "1" : 
            병음 = "Y" ;
            휴식 ;
        경우 "2" : 
            병음 = "E" ;
            휴식 ;
        경우「3」: 
            병음 = "S" ;
            휴식 ;
        경우 "4" :;
            휴식 ;
        케이스 "5" : 
            병음 = "W" ;
            휴식 ;
        케이스 "6" : 
            병음 = "L" ;
            휴식 ;
        케이스 "7" : 
            병음 = "Q" ;
            휴식 ;
        케이스 "8" : 
            병음 = "B" ;
            휴식 ;
        경우 "9" :;
            휴식 ; 
        } 
        반환 병음을; 

    } 
    
    
    공공  정적  무효 메인 (문자열 [] 인수)를 발생 BadHanyuPinyinOutputFormatCombination { 
        에서 System.out.println (toPinYinFirstUpper ( "赵波蓝은 '' ' )); 
    } 
}

 

추천

출처www.cnblogs.com/zblwyj/p/11653241.html