• 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);
        }
    }
  • 相关阅读:
    【Rust】问号的使用
    【Rust】可变数组
    【Rust】字符串
    【Rust】迭代中错误一
    jQuery之CSS模块,筛选模块,文档处理(CUD)模块,事件模总结 安静点
    jQuery事件委托(委派/代理) 安静点
    jQuery文档增删 安静点
    jQuery事件绑定与解绑 安静点
    jQuery爱好选择器 安静点
    jQuery之mouseover与mouseenter区别 安静点
  • 原文地址:https://www.cnblogs.com/zengsong-restService/p/3267238.html
Copyright © 2020-2023  润新知