Java 汉字转拼音(倒叙显示,过滤字符,字母,有数字添加到末尾)

汉字转拼音(倒叙显示,过滤字符,字母,有数字添加到末尾)

第三方架包下载地址:https://sourceforge.net/projects/pinyin4j/files/


package a;
import java.util.LinkedList;
import java.util.regex.Pattern;

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;

public class HanyuPinyinHelper {

    /** 
     * 将文字转为汉语拼音
     * @param chineselanguage 要转成拼音的中文
     */
    public String toHanyuPinyin(String ChineseLanguage){
        char[] cl_chars = ChineseLanguage.trim().toCharArray();
        StringBuffer hanyupinyin = new StringBuffer(); // 拼音
        StringBuffer number = new StringBuffer(); // 数字
        LinkedList linkedList = new LinkedList<>();
        HanyuPinyinOutputFormat defaultFormat = new HanyuPinyinOutputFormat();
        defaultFormat.setCaseType(HanyuPinyinCaseType.LOWERCASE);// 输出拼音全部小写
        defaultFormat.setToneType(HanyuPinyinToneType.WITHOUT_TONE);// 不带声调
        defaultFormat.setVCharType(HanyuPinyinVCharType.WITH_V) ;
        try {
            for (int i=0; i= 0; i--) {
        	hanyupinyin.append(linkedList.get(i)+".");
		}
        String fullStr = hanyupinyin.toString().substring(0 , hanyupinyin.length() -1);
        return fullStr + number;
    }
    
    public static void main(String[] args) {
        HanyuPinyinHelper hanyuPinyinHelper = new HanyuPinyinHelper() ;
        System.out.print(hanyuPinyinHelper.toHanyuPinyin("鑫淼焱犇骉朴"));
    }
}


运行结果:

po.biao.ben.yan.miao.xin123

参考链接:https://www.cnblogs.com/zh-1721342390/p/8276922.html

你可能感兴趣的:(JavaWeb,随笔,java技巧)