• java中汉字自动转换成拼音



    java中汉字自动转换成拼音

    1.需要下载jar包 pinyin4j.2.5.0.jar ,加入到WEB-INF下的lib里边,右键add to bulid path。

    2.[代码]PinYinUtil  

    1
    2
    3
    4
    5
    6
    7
    8
    9
    10
    11
    12
    13
    14
    15
    16
    17
    18
    19
    20
    21
    22
    23
    24
    25
    26
    27
    28
    29
    30
    31
    32
    33
    34
    35
    36
    37
    38
    39
    40
    41
    42
    43
    44
    45
    46
    47
    48
    49
    50
    51
    52
    53
    54
    55
    56
    57
    58
    59
    60
    61
    62
    63
    64
    65
    66
    67
    68
    69
    70
    71
    72
    73
    74
    75
    76
    77
    78
    79
    80
    81
    82
    83
    84
    85
    86
    87
    88
    89
    90
    91
    92
    93
    94
    95
    package oa.common.utils;
     
    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;
     
    /**
     * 拼音工具类
     *
     * @author lsf
     */
    public class PinYinUtil {
        /**
         * 将字符串中的中文转化为拼音,其他字符不变
         *
         * @param inputString
         * @return
         */
        public static String getPingYin(String inputString) {
            HanyuPinyinOutputFormat format = new HanyuPinyinOutputFormat();
            format.setCaseType(HanyuPinyinCaseType.LOWERCASE);
            format.setToneType(HanyuPinyinToneType.WITHOUT_TONE);
            format.setVCharType(HanyuPinyinVCharType.WITH_V);
     
            char[] input = inputString.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], format);
                        output += temp[0];
                    } else
                        output += java.lang.Character.toString(input[i]);
                }
            } catch (BadHanyuPinyinOutputFormatCombination 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);  
                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();  
        }  
        /** 
         * 获取汉字串拼音,英文字符不变 
         * @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);  
                for (int i = 0; i < arr.length; i++) {  
                        if (arr[i] > 128) {  
                                try {  
                                        pybf.append(PinyinHelper.toHanyuPinyinStringArray(arr[i], defaultFormat)[0]);  
                                } catch (BadHanyuPinyinOutputFormatCombination e) {  
                                        e.printStackTrace();  
                                }  
                        } else {  
                                pybf.append(arr[i]);  
                        }  
                }  
                return pybf.toString();  
        

    3.用到的时候,import com.XXX.PinYinUtil; 看自己的实际路径

    然后调用PinYinUtil.getFirstSpell(String chinese);

               PinYinUtil.getFullSpell(String chinese);

  • 相关阅读:
    Javascript学习笔记3 Javascript与BOM简介
    Javascript学习笔记2.3 Javascript与DOM实现动态表格效果
    Javascript学习笔记2.2 Javascript与DOM选项卡(滑动门)案例详解
    javascript是做什么的
    Javascript学习笔记2.1 Javascript与DOM简介
    Javascript学习笔记1 javascript的特点
    CSS3新增的选择器和属性
    DNSlog实现Mysql注入
    [转]Firefox+Burpsuite抓包配置(可抓取https)
    爬虫初窥day3:BeautifulSoup
  • 原文地址:https://www.cnblogs.com/limeiky/p/5391523.html
Copyright © 2020-2023  润新知