• pinyin4j 实现 中文和拼音之间转化


    maven依赖:
            <!-- https://mvnrepository.com/artifact/com.belerweb/pinyin4j -->
            <dependency>
                <groupId>com.belerweb</groupId>
                <artifactId>pinyin4j</artifactId>
                <version>2.5.0</version>
            </dependency>
    import lombok.extern.slf4j.Slf4j;
    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.exception.BadHanyuPinyinOutputFormatCombination;
    
    import java.io.*;
    
    /**
     * @Date 2020/7/2 14:08
     */
    @Slf4j
    public class ChineseToEnglish {
        /**
         * 获取汉字串拼音首字母,英文字符不变
         *
         * @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();
        }
        
        public static void main(String[] args) {
    //读取文件的位置 File pathFile
    = new File("H:\weixindownload\WeChat Files\wxid_h4rkelzyxk5t22\FileStorage\File\2020-07\不停电次数(1)(1)(1).xls"); if (pathFile.exists()) { System.out.println("exists"); InputStreamReader inputStreamReader = null; try { inputStreamReader = new InputStreamReader(new FileInputStream(pathFile), "UTF-8"); BufferedReader bufferedReader = new BufferedReader(inputStreamReader); String LineTxt = null; while ((LineTxt = bufferedReader.readLine()) != null) { String[] split = LineTxt.split(","); for (int i = 0; i <= 43; i++) { System.out.println("/** " + split[i] + " */"); System.out.println("@ExcelProperty("" + getFirstSpell(split[i]) + "")"); System.out.println("private String " + getFirstSpell(split[i]) + ";"); } //在这地方打个断点 System.out.println("123"); } } catch (UnsupportedEncodingException | FileNotFoundException e) { e.printStackTrace(); } catch (IOException e) { e.printStackTrace(); } } else { System.err.println("no exists"); } } }
  • 相关阅读:
    slots属性(省内存,限制属性的定义)
    自定制格式化方式format
    改变对象的字符串显示__str__repr
    __getattribute__和item系列
    授权(类似)
    双下划线开头的attr方法
    动态导入模块
    反射
    python的单下划线和双下划线
    在子类中调用父类的方法
  • 原文地址:https://www.cnblogs.com/lovetl/p/13343266.html
Copyright © 2020-2023  润新知