java使用pinyin4j汉字转全拼和首字母

个人记录。

<!-- https://mvnrepository.com/artifact/com.belerweb/pinyin4j -->
<dependency>
    <groupId>com.belerweb</groupId>
    <artifactId>pinyin4j</artifactId>
    <version>2.5.1</version>
</dependency>

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;

/**
 * 拼音处理
 * 全拼和首字母
 *
 * @author admin
 */
public class PinYinUtil {


    private static HanyuPinyinOutputFormat defaultFormat = null;

    static HanyuPinyinOutputFormat getHanyuPinyinOutputFormat() {
        if (null == defaultFormat) {
            defaultFormat = new HanyuPinyinOutputFormat();
        }
        defaultFormat.setCaseType(HanyuPinyinCaseType.LOWERCASE);
        defaultFormat.setToneType(HanyuPinyinToneType.WITHOUT_TONE);
        return defaultFormat;
    }

    /**
     * 获取汉字串拼音首字母,英文字符不变,特殊字符过滤
     *
     * @param chinese 汉字串
     * @return 汉语拼音首字母
     */
    public static String getFirstSpell(String chinese) {
        StringBuilder firstSpell = new StringBuilder();
        char[] arr = chinese.toCharArray();
        for (char c : arr) {
            if (c > 128) {
                try {
                    String[] temp = PinyinHelper.toHanyuPinyinStringArray(c, getHanyuPinyinOutputFormat());
                    if (temp != null && temp.length >= 1) {
                        firstSpell.append(temp[0].charAt(0));
                    }
                } catch (BadHanyuPinyinOutputFormatCombination e) {
                    e.printStackTrace();
                }
            } else {
                firstSpell.append(c);
            }
        }
        return firstSpell.toString();
    }

    /**
     * 获取汉字串拼音,英文字符不变,特殊字符过滤
     *
     * @param chinese 汉字串
     * @return 汉语拼音
     */
    public static String getFullSpell(String chinese) {
        StringBuilder fullSpell = new StringBuilder();
        char[] arr = chinese.toCharArray();
        for (char c : arr) {
            if (c > 128) {
                try {
                    String[] temp = PinyinHelper.toHanyuPinyinStringArray(c, getHanyuPinyinOutputFormat());
                    if (temp != null && temp.length >= 1) {
                        fullSpell.append(temp[0]);
                    }
                } catch (BadHanyuPinyinOutputFormatCombination e) {
                    e.printStackTrace();
                }
            } else {
                fullSpell.append(c);
            }
        }
        return fullSpell.toString();
    }

    public static void main(String[] args) {
        String fullSpell = getFullSpell("A、B1测试");
        String firstSpell = getFirstSpell("A、B1测试");
        System.err.println(fullSpell);
        System.err.println(firstSpell);
    }

}
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值