pinyin4j记录

    
    /*pinyin4j是将汉字转化为拼音的类库,比如"中国"转化为"zhongguo"*/
    @Test
    public void test01(){
        try {
            HanyuPinyinOutputFormat format=new HanyuPinyinOutputFormat();
            format.setCaseType(HanyuPinyinCaseType.LOWERCASE);
            //小写
            format.setToneType(HanyuPinyinToneType.WITHOUT_TONE);
            //不要声调
            format.setVCharType(HanyuPinyinVCharType.WITH_U_AND_COLON);
            //u和v的取舍
            String[] strs=PinyinHelper.toHanyuPinyinStringArray('中',format);
            for(String str:strs){
                System.out.println(str);
            }
            String kouhao="我是中国人我要抵制日货";
            StringBuffer re=new StringBuffer();
            for(int i=0;i<kouhao.length();i++){
                re.append(PinyinHelper.toHanyuPinyinStringArray(kouhao.charAt(i),format)[0]);
            }
            System.out.println(re);
        } catch (BadHanyuPinyinOutputFormatCombination e) {
            e.printStackTrace();
        }
    }
    
    @Test
    public void test02(){
        HanyuPinyinOutputFormat format=new HanyuPinyinOutputFormat();
        format.setCaseType(HanyuPinyinCaseType.LOWERCASE);
        //小写
        format.setToneType(HanyuPinyinToneType.WITHOUT_TONE);
        //不要声调
        format.setVCharType(HanyuPinyinVCharType.WITH_U_AND_COLON);
        //u和v的取舍
        String kouhao="支持国产,抵制日货,fuck japan";
        StringBuffer re=new StringBuffer();
        System.out.println(PinyinUtil.str2Pinyin(kouhao, ","));
        System.out.println(PinyinUtil.strFirst2Pinyin(kouhao));
    }

//pinyin4j工具类封装PinyinUtil.java

    public class PinyinUtil {

    private static HanyuPinyinOutputFormat format=new HanyuPinyinOutputFormat();
    static{
        format.setCaseType(HanyuPinyinCaseType.LOWERCASE);
        format.setToneType(HanyuPinyinToneType.WITHOUT_TONE);
        format.setVCharType(HanyuPinyinVCharType.WITH_V);
    }
    
    public static String str2Pinyin(String str,String fill){
        try {
            StringBuffer sb=new StringBuffer();
            if(fill==null) fill="";
            boolean iscn=true;
            for(int i=0;i<str.length();i++){
                char c=str.charAt(i);
                if(i>0&&iscn) {
                    sb.append(fill);
                }
                if(c==' '){
                    sb.append(fill);
                }
                //1.判断c是不是中文,百度"unicode"
                if(c>='\u4e00'&&c<='\u9fa5'){
                    iscn=true;
                    sb.append(PinyinHelper.toHanyuPinyinStringArray(c,format)[0]);
                }else{
                    //不是中文
                    if((c>='a'&&c<='z')||(c>='A'&&c<='Z')){
                        sb.append(c);
                    }
                    iscn=false;
                }
            }
            return sb.toString();
        } catch (BadHanyuPinyinOutputFormatCombination e) {
            e.printStackTrace();
        }
        return null;
    }
    /**将汉字拼音第一个字母组成新字符串*/
    public static String strFirst2Pinyin(String str){
        try {
            StringBuffer sb=new StringBuffer();
            boolean iscn=true;
            for(int i=0;i<str.length();i++){
                char c=str.charAt(i);
                //1.判断c是不是中文,百度"unicode"
                if(c>='\u4e00'&&c<='\u9fa5'){
                    iscn=true;
                    sb.append(PinyinHelper.toHanyuPinyinStringArray(c,format)[0].charAt(0));
                }else{
                    //不是中文
                    
                }
            }
            return sb.toString();
        } catch (BadHanyuPinyinOutputFormatCombination e) {
            e.printStackTrace();
        }
        return null;
    }


你可能感兴趣的:(pinyin4j记录)