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

      在项目开发中,有时会遇到要将汉字转换为拼音形式,然后再进行处理的业务,方便数据处理等等.

      这里提供一个转换工具类,使用的是pinyin的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;
    import org.slf4j.Logger;
    import org.slf4j.LoggerFactory;
    
    /**
     * Created with IntelliJ IDEA
     * User: Butterfly Killer
     * Date: 2017/3/9 10:39
     * <p>
     * Description: 汉字转拼音工具
     * </p>
     */
    public class ChineseToPinyinUtils {
    
        private static final Logger LOGGER = LoggerFactory.getLogger(ChineseToPinyinUtils.class.getName());
    
        /**
         * 将汉字转为小写的拼音 [全拼]
         * <p>
         * 例如: "测试",转换结果为 'ceshi'
         * </p>
         *
         * @param chines 汉字
         * @return pinyin 拼音
         */
        public static String toPinyin(String chines) {
            String pinyin = "";
            char[] chinesChar = chines.toCharArray();
            String[] template;
            for (char ch : chinesChar) {
                if (Character.toString(ch).matches("[\u4E00-\u9FA5]+")) {
                    template = getCharToStringArray(ch);
                    pinyin += template[0];
                } else {
                    pinyin += Character.toString(ch);
                }
            }
            return pinyin;
        }
    
        /**
         * 提取汉字的第一个拼音的大写形式字母
         * <p>
         * 例如: "测试" 转换结果为 'CS'
         * </p>
         *
         * @param chines 汉字
         * @return pinyin 首位拼音大写组合
         */
        public static String toFirstCapitalPinyin(String chines) {
            String pinyin = "";
            for (int i = 0; i < chines.length(); i++) {
                char word = chines.charAt(i);
                String[] pinyinArray = getCharToStringArray(word);
                if (null != pinyinArray) {
                    pinyin += pinyinArray[0].charAt(0);
                } else {
                    pinyin += word;
                }
            }
            return pinyin.toUpperCase();
        }
    
        /**
         * 将汉字转换为拼音全拼,首字母大写
         * <p>
         * 例如: "测试" 转换结果为 'CeShi'
         * </p>
         *
         * @param chines 汉字
         * @return pinyin
         */
        public static String toStandardPinyin(String chines) {
            String pinyin = "";
            char[] chinesChar = chines.toCharArray();
            for (char cher : chinesChar) {
                String param = String.valueOf(cher);
                // 汉字符判断
                if (param.matches("[\u4e00-\u9fa5]")) {
                    pinyin += getFirstCharacterUpperCast(cher);
                } else {
                    pinyin += param;
                }
            }
            return pinyin;
        }
    
        /**
         * 将字符串转移为ASCII码
         *
         * @param chines 汉字
         * @return ascII
         */
        public static String toAscII(String chines) {
            String ascII = "";
            byte[] chinesBytes = chines.getBytes();
            for (byte ch : chinesBytes) {
                ascII += Integer.toHexString(ch & 0xff);
            }
            return ascII;
        }
    
        /**
         * char转为String组
         *
         * @param cher char对象
         * @return String组
         */
        private static String[] getCharToStringArray(char cher) {
            HanyuPinyinOutputFormat hanyuPinyinOutputFormat = new HanyuPinyinOutputFormat();
            hanyuPinyinOutputFormat.setCaseType(HanyuPinyinCaseType.LOWERCASE); // 全小写
            hanyuPinyinOutputFormat.setToneType(HanyuPinyinToneType.WITHOUT_TONE); // 无音调
            hanyuPinyinOutputFormat.setVCharType(HanyuPinyinVCharType.WITH_V); // 'ü' 换成 "v"
            String[] words = new String[0];
            try {
                words = PinyinHelper.toHanyuPinyinStringArray(cher, hanyuPinyinOutputFormat);
            } catch (BadHanyuPinyinOutputFormatCombination e) {
                LOGGER.error("BAD HanYu Pinyin Output Format Combination : {}", e.getMessage());
            }
            return words;
        }
    
        // 将单个汉字转换为首字母大写形式的拼音
        private static String getFirstCharacterUpperCast(char word) {
            String firstUpper = "";
            String[] pinyinArray = getCharToStringArray(word);
            if (null != pinyinArray) {
                char[] pinyinCharacters = pinyinArray[0].toCharArray();
                for (int i = 0; i < pinyinCharacters.length; i++) {
                    if (0 == i) {
                        firstUpper += String.valueOf(pinyinCharacters[i]).toUpperCase();
                    } else {
                        firstUpper += pinyinCharacters[i];
                    }
                }
            }
            return firstUpper;
        }
    
        public static void main(String[] args) {
            String chine = "生存或者死亡,这是个问题.";
            System.out.println(toStandardPinyin(chine));
        }
    }

    =========================TODO=========================

  • 相关阅读:
    超简单留言版
    DirectorySearCh的PropertiesToLoad所有属性
    "Asp.Net Web Api MediaTypeFormatter Error for xwwwformurlencoded data" 解决方法
    关于 NPOI 报 Invalid column index (256). Allowable column range for BIFF8 is (0..255) or ('A'..'IV') 错误的解决办法
    Autofac 的构造函数注入方式
    VirtualBox 内的 Ubuntu Server 虚拟机网络配置
    AngularJS 中设置 AJAX get 请求不缓存的方法
    IIS中使用LocalDB遇到错误:error 50,Local Database Runtime error occurred.的解决办法
    升级 DNX 和 DNVM
    规约模式学习总结
  • 原文地址:https://www.cnblogs.com/SummerinShire/p/6531489.html
Copyright © 2011-2022 走看看