zoukankan      html  css  js  c++  java
  • java 汉字转拼音

    先决条件:

    pinyin4j.jar(Pinyin4j是一个流行的Java库,支持中文字符和拼音之间的转换。拼音输出格式可以定制。)

    下载地址:http://pan.baidu.com/share/link?shareid=3958741959&uk=3792676205

    PinyinUtil.java

    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 PinyinUtil {
    
        /**
         * 将字符串中的中文转化为拼音,其他字符不变
         * 
         * @param inputStr
         * @return 汉字拼音
         */
        public static String getPinYin(String inputStr) {
            HanyuPinyinOutputFormat hpFormat = new HanyuPinyinOutputFormat();
            hpFormat.setCaseType(HanyuPinyinCaseType.LOWERCASE);// 小写
            hpFormat.setToneType(HanyuPinyinToneType.WITHOUT_TONE);
            hpFormat.setVCharType(HanyuPinyinVCharType.WITH_V);
            char[] input = inputStr.trim().toCharArray();
            String output = "";
            try {
                for (int i = 0; i < input.length; i++) {
                    if (java.lang.Character.toString(input[i]).matches(
                            "[\u4E00-\u9FA5]+")) {
                        String[] temp = PinyinHelper.toHanyuPinyinStringArray(
                                input[i], hpFormat);
                        output += temp[0];
                    } else {
                        output += input[i];
                    }
                }
            } catch (Exception e) {
                e.printStackTrace();
            }
            return output;
        }
    
        /**
         * 获取汉字串拼音首字母,英文字符不变
         * 
         * @param chinese
         *            汉字串
         * @return 汉语拼音首字母
         */
        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);
            defaultFormat.setVCharType(HanyuPinyinVCharType.WITH_V);
    
            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));
                        } else {
                            pybf.append(arr[i]);
                        }
                    } catch (BadHanyuPinyinOutputFormatCombination e) {
                        e.printStackTrace();
                    }
                }
            }
            return pybf.toString().replaceAll("\W", "").trim();
        }
    
        /**
         * 获取汉字串拼音,英文字符不变
         * 
         * @param chinese
         *            汉字串
         * @return 汉语拼音
         */
        public static String getFullSpell(String chinese) {
            StringBuffer pybf = new StringBuffer();
            char[] arr = chinese.toCharArray();
    
            HanyuPinyinOutputFormat defaultFormat = new HanyuPinyinOutputFormat();
            defaultFormat.setCaseType(HanyuPinyinCaseType.LOWERCASE);
            defaultFormat.setToneType(HanyuPinyinToneType.WITHOUT_TONE);
            defaultFormat.setVCharType(HanyuPinyinVCharType.WITH_V);
    
            try {
                for (int i = 0; i < arr.length; i++) {
                    if (arr[i] > 128) {
                        pybf.append(PinyinHelper.toHanyuPinyinStringArray(arr[i],
                                defaultFormat)[0]);
                    } else {
                        pybf.append(arr[i]);
                    }
                }
            } catch (BadHanyuPinyinOutputFormatCombination e) {
                e.printStackTrace();
            }
            return pybf.toString();
        }

    test.java

    public class test {    
            public static void main(String[] args) {
            String result = getPinYin("赠送");
            System.out.println(result);
        }
    }
  • 相关阅读:
    LIBTIFF 配置 (vs2010 + win8 + 32位 )
    minGW、cygwin、gnuwin32 介绍
    LabVIEW发布功能总结
    LabVIEW新手5大错误
    专业功放测试:主要性能指标&信噪比测量
    常用低压电器的主要种类和用途
    LabVIEW是一种通用的编程语言吗?
    LabVIEW TCP/IP 断开重连问题
    LabVIEW 的bool(布尔)按键机械属性
    LabVIEW 远程控制VI
  • 原文地址:https://www.cnblogs.com/zengsong-restService/p/3267238.html
Copyright © 2011-2022 走看看