Java之拼音工具类的使用

在自动生成字段key时,字段的key可以设置成使用字段名的拼音。

拼音工具类实现如下:

package com.cmit.kapok.system.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.exception.BadHanyuPinyinOutputFormatCombination;

public class PinYinUtil {

    /**
     * 汉字转为拼音
     * @param chinese
     * @return
     */
    public static String toPinyin(String chinese){
        chinese = chinese.replace("/","_");
        int index1 = chinese.indexOf("(");
        if(index1>=0){
            chinese = chinese.substring(0,index1);
        }else{
            int index2 = chinese.indexOf("(");
            if(index2>=0){
                chinese = chinese.substring(0,index2);
            }
        }
        String pinyinStr = "";
        char[] newChar = chinese.toCharArray();
        HanyuPinyinOutputFormat defaultFormat = new HanyuPinyinOutputFormat();
        defaultFormat.setCaseType(HanyuPinyinCaseType.LOWERCASE);
        defaultFormat.setToneType(HanyuPinyinToneType.WITHOUT_TONE);
        for (int i = 0; i < newChar.length; i++) {
            if (newChar[i] > 128) {
                try {
                    if(pinyinStr.length()>0 && !pinyinStr.substring(pinyinStr.length()-1).equals("_")){
                        pinyinStr += "_";
                    }
                    pinyinStr += PinyinHelper.toHanyuPinyinStringArray(newChar[i], defaultFormat)[0];
                } catch (BadHanyuPinyinOutputFormatCombination e) {
                    e.printStackTrace();
                }
            }else{
                pinyinStr += newChar[i];
            }
        }
        return pinyinStr;
    }

    /**
     * 获取字符串拼音的第一个字母
     * @param chinese
     * @return
     */
    public static String toFirstChar(String chinese){
        String pinyinStr = "";
        char[] newChar = chinese.toCharArray();  //转为单个字符
        HanyuPinyinOutputFormat defaultFormat = new HanyuPinyinOutputFormat();
        defaultFormat.setCaseType(HanyuPinyinCaseType.LOWERCASE);
        defaultFormat.setToneType(HanyuPinyinToneType.WITHOUT_TONE);
        for (int i = 0; i < newChar.length; i++) {
            if (newChar[i] > 128) {
                try {
                    pinyinStr += PinyinHelper.toHanyuPinyinStringArray(newChar[i], defaultFormat)[0].charAt(0);
                } catch (BadHanyuPinyinOutputFormatCombination e) {
                    e.printStackTrace();
                }
            }else{
                pinyinStr += newChar[i];
            }
        }
        return pinyinStr;
    }

    public static void main(String[] args){
        System.out.println(toPinyin("是否按宽带标准收费(如100M/100元)"));
    }
}

调用时为了保证字段key的唯一性,可以在后面加上系统时间和随机数。

int randomInt = (int)(1+Math.random()*10);
String fieldKey = PinYinUtil.toPinyin(miitTemplateField.getFieldName())+"_"+System.currentTimeMillis()+"_"+randomInt;

 

posted @ 2022-11-16 16:38  罗毅豪  阅读(295)  评论(0编辑  收藏  举报