• java汉字转拼音工具类


      添加依赖

    <dependency>
                <groupId>com.belerweb</groupId>
                <artifactId>pinyin4j</artifactId>
                <version>2.5.1</version>
    </dependency>

    工具类代码:

    public class PinYinUtils {
    
        public static HanyuPinyinOutputFormat PINYIN_FORMAT;
        static {
    
            PINYIN_FORMAT = new HanyuPinyinOutputFormat();
            /**
             * 大小写设置
             *      LOWERCASE:小写
             *      UPPERCASE:大写
             */
            PINYIN_FORMAT.setCaseType(HanyuPinyinCaseType.LOWERCASE);
            /**
             * 输出音标设置
             *
             * WITH_TONE_MARK:直接用音标符(VCharType必须设置WITH_U_UNICODE,否则会抛出异常)
             * WITH_TONE_NUMBER:1-4数字表示音标
             * WITHOUT_TONE:没有音标
             */
            PINYIN_FORMAT.setToneType(HanyuPinyinToneType.WITHOUT_TONE);
            /**
             * 特殊音符ü的设置
             * WITH_U_AND_COLON:用u表示(没有设置默认用u表示)
             * WITH_V:用v表示
             * WITH_U_UNICODE:用ü表示
             */
            PINYIN_FORMAT.setVCharType(HanyuPinyinVCharType.WITH_U_UNICODE);
        }
    
        /**
         *  取汉字的拼音首字母
         * @param chinese
         * @return
         */
        public static String toFirstPinYin(String chinese){
            StringBuilder result = new StringBuilder();
            //将字符串转成字符数组
            char[] chars = chinese.toCharArray();
            try {
                for (char c : chars) {
                    //是中文则进行转换
                    if(String.valueOf(c).matches("[u4e00-u9fa5]+")){
                        String[] pinyinStr = PinyinHelper.toHanyuPinyinStringArray(c, PINYIN_FORMAT);
                        result.append(pinyinStr[0].charAt(0));//取每个中文的第一个拼音字母
                    }else {
                        result.append(c);
                    }
                }
            } catch (BadHanyuPinyinOutputFormatCombination badHanyuPinyinOutputFormatCombination) {
                badHanyuPinyinOutputFormatCombination.printStackTrace();
            }
            return result.toString();
        }
    
        /**
         *  汉字转拼音小写
         * @param chinese
         * @return
         */
        public static String toPinYin(String chinese){
            //创建返回对象
            StringBuilder result = new StringBuilder();//将字符串转成字符数组
            char[] chars = chinese.toCharArray();
            try {
                for (char c : chars) {
                    //是中文则进行转换
                    if(String.valueOf(c).matches("[u4e00-u9fa5]+")){
                        String[] pinyinStr = PinyinHelper.toHanyuPinyinStringArray(c, PINYIN_FORMAT);
    //                    result.append(pinyinStr[0].charAt(0));//取每个中文的第一个拼音字母
                        result.append(pinyinStr[0]);
                    }else {
                        result.append(c);
                    }
                }
            } catch (BadHanyuPinyinOutputFormatCombination badHanyuPinyinOutputFormatCombination) {
                badHanyuPinyinOutputFormatCombination.printStackTrace();
            }
            return result.toString();
        }
        /**
         *  汉字转拼音每个字符串的第一个字母大写其余小写
         * @param chinese
         * @return
         */
        public static String toUpperStringsFirstCharPinYin(String chinese){
            //创建返回对象
            StringBuilder result = new StringBuilder();//将字符串转成字符数组
            char[] chars = chinese.toCharArray();
            try {
                for (char c : chars) {
                    //是中文则进行转换
                    if(String.valueOf(c).matches("[u4e00-u9fa5]+")){
                        String[] pinyinStr = PinyinHelper.toHanyuPinyinStringArray(c, PINYIN_FORMAT);
    //                    result.append(pinyinStr[0].charAt(0));//取每个中文的第一个拼音字母
                        String c1 = String.valueOf(pinyinStr[0]);
                        result.append(c1.substring(0,1).toUpperCase()).append(c1.substring(1));
                    }else {
                        result.append(c);
                    }
                }
            } catch (BadHanyuPinyinOutputFormatCombination badHanyuPinyinOutputFormatCombination) {
                badHanyuPinyinOutputFormatCombination.printStackTrace();
            }
            return result.toString();
        }
    
        public static void main(String[] args) {
           
            String str = "拼音工具lv";
            String pinYin = toUpperStringsFirstCharPinYin(str);
            System.out.println(pinYin);
        }
    }
  • 相关阅读:
    StreamSets学习系列之StreamSets是什么?
    [转]How to mouse hover using Blue prism on a web page
    [转]UiPath: How to Capture a Mouse Event on Hover Menus?
    [转]VB.net中 excel 的range方法
    [转]Paste from Excel into C# app, retaining full precision
    [转]uipath team svn
    [转]RPA认证 Developer UIPath Certificate,细说uipath认证学习,Online Quiz和Practical Exam项目详解
    [转]UiPath教程:UiPath及其组件介绍
    [转]Introduction
    [转]How to enable macros in Excel 2016, 2013, and 2010
  • 原文地址:https://www.cnblogs.com/8593l/p/12528471.html
Copyright © 2020-2023  润新知