zoukankan      html  css  js  c++  java
  • 汉字转拼音(pinyin4j-2.5.0.jar)

    import net.sourceforge.pinyin4j.PinyinHelper;
    import net.sourceforge.pinyin4j.format.HanyuPinyinCaseType;
    import net.sourceforge.pinyin4j.format.HanyuPinyinOutputFormat;
    import net.sourceforge.pinyin4j.format.HanyuPinyinToneType;
    import net.sourceforge.pinyin4j.format.HanyuPinyinVCharType;
    import net.sourceforge.pinyin4j.format.exception.BadHanyuPinyinOutputFormatCombination;
    
    public class PinYinUtils {
        
        
        public static String toPinYin(String string){
            char[] c = string.toCharArray();
            StringBuffer stringBuffer = new StringBuffer();
            for(int i = 0; i< c.length; i++){
                stringBuffer.append(toChar(c[i]));
            }
            return stringBuffer.toString();
        }
        
        private static String toChar(char c){
            HanyuPinyinOutputFormat format = new HanyuPinyinOutputFormat();
            format.setCaseType(HanyuPinyinCaseType.LOWERCASE);
            format.setToneType(HanyuPinyinToneType.WITHOUT_TONE);
            format.setVCharType(HanyuPinyinVCharType.WITH_V);
            try{
                String[] pinYin = PinyinHelper.toHanyuPinyinStringArray(c, format);
                if(pinYin != null){
                    return pinYin[0];
                }
            }
            catch(BadHanyuPinyinOutputFormatCombination ex){
                ex.printStackTrace();
            }
            return String.valueOf(c);
        }
    }
  • 相关阅读:
    字符匹配算法之KMP
    rabbitmq_hearbeat
    rabbitmq_config
    postgres SQL编译过程
    postgres启动过程分析
    postgres源码目录结构
    Js两种post方式(转)
    PHP-MySQL,PHP-MySQLi,PDO的差异
    CSS属性中Display与Visibility的不同
    PHP中include路径修改
  • 原文地址:https://www.cnblogs.com/rubekid/p/4850284.html
Copyright © 2011-2022 走看看