0
点赞
收藏
分享

微信扫一扫

java--->中文(汉子)转换拼音

yeamy 2022-02-16 阅读 108

简介

        在我们使用各种APP的搜索功能时,既可以根据中文搜索,也可以根据拼音搜索。

        根据业务的需求来应对。

实现

        pinyin4j  -- 引入maven依赖

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

Java代码:

package com.example.thread.projectTest;

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;

import java.io.UnsupportedEncodingException;

public class Pinyin {

    /**
     * @param chinese (字符串 汉字)
     * @return 汉字转拼音 其它字符不变
     */
    public static String getAllSpell(String chinese){
        StringBuffer buffer = new StringBuffer();
        HanyuPinyinOutputFormat formart = new HanyuPinyinOutputFormat();
        formart.setCaseType(HanyuPinyinCaseType.LOWERCASE);
        formart.setToneType(HanyuPinyinToneType.WITHOUT_TONE);
        formart.setVCharType(HanyuPinyinVCharType.WITH_V);
        char[] array = chinese.trim().toCharArray();
        try {
            for (int i = 0;i < array.length; i++) {
                char t = array[i];
                //匹配是否是中文
                if(Character.toString(t).matches("[\\u4e00-\\u9fa5]")){ 
                    String[] temp = PinyinHelper.toHanyuPinyinStringArray(t,formart);
                    buffer.append(temp[0]);
                }else{
                    buffer.append(t);
                }
            }
        } catch (BadHanyuPinyinOutputFormatCombination e) {
            e.printStackTrace();
        }
        return buffer.toString().replaceAll("\\W", "").trim();
    }

    /**
     * 获取汉字串拼音首字母,英文字符不变
     * @param chinese 汉字串
     * @return 汉语拼音首字母小写  大写可以自动转换
     */
    public static String getFirstSpell(String chinese) {
        StringBuffer buffer = new StringBuffer();
        char[] array = chinese.toCharArray();
        HanyuPinyinOutputFormat defaultFormat = new HanyuPinyinOutputFormat();
        defaultFormat.setCaseType(HanyuPinyinCaseType.LOWERCASE);
        defaultFormat.setToneType(HanyuPinyinToneType.WITHOUT_TONE);
        for (int i = 0; i < array.length; i++) {
            if (array[i] > 128) {
                try {
                    String[] temp = PinyinHelper.toHanyuPinyinStringArray(array[i], defaultFormat);
                    if (temp != null) {
                        // 截取首字母 小写
                        buffer.append(temp[0].charAt(0));
                        /* 字母大写
                        char charAt = temp[0].charAt(0);
                        charAt -= 32; // 改变字符的编码值
                        buffer.append(charAt);*/
                    }
                } catch (BadHanyuPinyinOutputFormatCombination e) {
                    e.printStackTrace();
                }
            } else {
                buffer.append(array[i]);
            }
        }
        return buffer.toString().replaceAll("\\W", "").trim();
    }

}

测试一下:

 

举报

相关推荐

0 条评论