字符串转成拼音

public class ChineseAndPinyinUtils {

    private ChineseAndPinyinUtils() {}

    /**
     * @param chinese (字符串 汉字)
     * @return 汉字转拼音 其它字符不变
     */
    public static String getPinyin(String chinese){
        HanyuPinyinOutputFormat formart = new HanyuPinyinOutputFormat();
        //拼音输出默认为小写,公共方法不做限制
        //下面设置输出拼音为大写
        //formart.setCaseType(HanyuPinyinCaseType.UPPERCASE);
        //拼音输出不带声调,否则带声调的字母乱码
        formart.setToneType(HanyuPinyinToneType.WITHOUT_TONE);
        //字母'ü' 输出为 'v',否则也是乱码
        formart.setVCharType(HanyuPinyinVCharType.WITH_V);
        char[] arrays = chinese.trim().toCharArray();
        String result = "";
        try {
            for (int i=0;i<arrays.length;i++) {
                char ti = arrays[i];
                if(Character.toString(ti).matches("[\\u4e00-\\u9fa5]")){ //匹配是否是中文
                    String[] temp = PinyinHelper.toHanyuPinyinStringArray(ti,formart);
                    result += temp[0];
                }else{
                    result += ti;
                }
            }
        } catch (BadHanyuPinyinOutputFormatCombination e) {
            e.printStackTrace();
        }
        return result;
    }
}
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值