Java实现将中文转成拼音和ASCII码

最近项目上遇到一个需求:模糊搜索姓名时既可以输入汉字也可以输入拼音进行搜索,这就要求在保存姓名时也要将其拼音保存起来。

好了,废话不说了,直接上代码!

实现步骤:

1、pom.xml配置文件引入jar包

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

2、将代码实现逻辑封装成一个工具类

package minyuantec.backupsystem.action;

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 PinyinUtil {

    /**
     * 将汉字转换为全拼
     * 
     * @param src
     * @return String
     */
    private static String getPinYin(String src) {
        char[] t1 = src.toCharArray();
        String[] t2 = new String[t1.length];
        // 设置汉字拼音输出的格式
        HanyuPinyinOutputFormat t3 = new HanyuPinyinOutputFormat();
        t3.setCaseType(HanyuPinyinCaseType.LOWERCASE);
        t3.setToneType(HanyuPinyinToneType.WITHOUT_TONE);
        t3.setVCharType(HanyuPinyinVCharType.WITH_V);
        String t4 = "";
        int t0 = t1.length;
        try {
            for (int i = 0; i < t0; ++i) {
                // 判断是否为汉字字符
                if (Character.toString(t1[i]).matches("[\\u4E00-\\u9FA5]+")) {
                    t2 = PinyinHelper.toHanyuPinyinStringArray(t1[i], t3);// 将汉字的几种全拼都存到t2数组中
                    t4 += t2[0];// 取出该汉字全拼的第一种读音并连接到字符串t4后
                } else {
                    // 如果不是汉字字符,直接取出字符并连接到字符串t4后
                    t4 = t4 + Character.toString(t1[i]);
                }
            }
        } catch (BadHanyuPinyinOutputFormatCombination var8) {
            var8.printStackTrace();
        }
        return t4;
    }

    /**
     * 中文汉字转成拼音首字母大写
     * 
     * @param str
     * @return String
     */
    public static String testPinyinInitial(String name){
        String pinyinInitial = "";
        try {
            char[] charArray = name.toCharArray();
            StringBuilder pinyin = new StringBuilder();
            HanyuPinyinOutputFormat defaultFormat = new HanyuPinyinOutputFormat();
            // 设置大小写格式
            defaultFormat.setCaseType(HanyuPinyinCaseType.UPPERCASE);
            // 设置声调格式:
            defaultFormat.setToneType(HanyuPinyinToneType.WITHOUT_TONE);
            for (int i = 0; i < charArray.length; i++) {
                // 匹配中文,非中文转换会转换成null
                if (Character.toString(charArray[i]).matches("[\\u4E00-\\u9FA5]+")) {
                    String[] hanyuPinyinStringArray = PinyinHelper
                            .toHanyuPinyinStringArray(charArray[i], defaultFormat);
                    if (hanyuPinyinStringArray != null) {
                        pinyin.append(hanyuPinyinStringArray[0].charAt(0));
                    }
                }
            }
            pinyinInitial = pinyin.toString();
            return pinyinInitial;
        } catch (BadHanyuPinyinOutputFormatCombination e) {
            // TODO: handle exception
        }
        return pinyinInitial;
        
    }
    
    //中文汉字转成ASCII码
    public static String getCnASCII(String cnStr) {
        StringBuffer strBuf = new StringBuffer();
        // 将字符串转换成字节序列
        byte[] bGBK = cnStr.getBytes();
        for (int i = 0; i < bGBK.length; i++) {
            // 将每个字符转换成ASCII码
            strBuf.append(Integer.toHexString(bGBK[i] & 0xff) + " ");
        }
        return strBuf.toString();
    }

    public static void main(String[] args) {
        String str = "你好世界";
        String pinyin = getPinYin(str);
        System.out.println(pinyin);
        String pinyinInitial = testPinyinInitial(str);
        System.out.println(pinyinInitial);
        String ascii = getCnASCII(str);
        System.out.println(ascii);

    }

}

打印结果为:

nihaoshijie
NHSJ
e4 bd a0 e5 a5 bd e4 b8 96 e7 95 8c 

 

欢迎关注微信公众号【Java典籍】,收看更多Java技术干货!

  ▼微信扫一扫下图↓↓↓二维码关注

 

posted @ 2020-04-24 19:02  冰湖一角  阅读(1224)  评论(1编辑  收藏  举报