获取汉字串拼音首字母,英文字符不变

   //  获取汉字串拼音首字母,英文字符不变      
    public static String getFirstSpell(String chinese) {  
        StringBuffer pybf = new StringBuffer();  
        char[] arr = chinese.toCharArray();  
        HanyuPinyinOutputFormat defaultFormat = new HanyuPinyinOutputFormat();  
        defaultFormat.setCaseType(HanyuPinyinCaseType.LOWERCASE);  
        defaultFormat.setToneType(HanyuPinyinToneType.WITHOUT_TONE);  
        for (int i = 0; i < arr.length; i++) {  
            if (arr[i] > 128) {  
                try {  
                    String[] temp = PinyinHelper.toHanyuPinyinStringArray(arr[i], defaultFormat);  
                    if (temp != null) {  
                        pybf.append(temp[0].charAt(0));  
                    }  
                } catch (BadHanyuPinyinOutputFormatCombination e) {  
                     e.printStackTrace();  
                }  
            } else {  
                pybf.append(arr[i]);  
            }  
        }  
        return pybf.toString().replaceAll("\\W", "").trim();  
    }  
   

没有更多推荐了,返回首页