JAVA实现中文转拼音 / 与阿拉伯数字之间的转换

**pinyin4j-2.5.0.jar**

package com.jalor.others;

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

    public static void main(String[] args) {
        
        System.out.println(toPinyin("嘉乐集团"));

    }

    public static String toPinyin(String fileString) {
        HanyuPinyinOutputFormat format = new HanyuPinyinOutputFormat();
        format.setCaseType(HanyuPinyinCaseType.LOWERCASE);
        format.setToneType(HanyuPinyinToneType.WITHOUT_TONE);
        format.setVCharType(HanyuPinyinVCharType.WITH_V);
        char[] input = fileString.trim().toCharArray();
        String output = "";
        try {
            for (int i = 0; i < input.length; i++) {
                if (Character.toString(input[i]).matches("[\\u4E00-\\u9FA5]+")) { // 判断字符是否是中文
                    // toHanyuPinyinStringArray 如果传入的不是汉字,就不能转换成拼音,那么直接返回null
                    // 由于中文有很多是多音字,所以这些字会有多个String,在这里我们默认的选择第一个作为pinyin
                    String[] temp = PinyinHelper.toHanyuPinyinStringArray(input[i], format);
                    output += temp[0];
                } else {
                    output += Character.toString(input[i]);
                }
            }
        } catch (BadHanyuPinyinOutputFormatCombination e) {
            e.printStackTrace();
            // Log.v(TAG, "BadHanyuPinyinOutputFormatCombination");
        }
        return output;
    }

}


**扩展内容**
 

package com.jalor;

/**
 * 阿拉伯数字与中文之间的转换
 * @author <a href="mailto:lenian@syjalor.com">lenian</a>
 * @version 2019年9月3日
 */
public class ABC {

    public static void main(String[] args) {

        System.out.println(toChinese("1001011"));
        System.out.println(toChinese2("1008611"));
        
    }

    // 阿拉伯数字转成中文数字(¥)
    private static String toChinese(String str) {
        String[] s1 = { "零", "一", "二", "三", "四", "五", "六", "七", "八", "九" };
        String[] s2 = { "十", "百", "千", "万", "十", "百", "千", "亿", "十", "百", "千" };
        String result = "";
        int n = str.length();
        for (int i = 0; i < n; i++) {
            int num = str.charAt(i) - '0';
            if (i != n - 1 && num != 0) {
                result += s1[num] + s2[n - 2 - i];
            }
            else {
                result += s1[num];
            }
        }
        return result;
    }

    // 将中文转换成阿拉伯数字
    private static String toChinese2(String str) {
        String[] s2 = { "零", "一", "二", "三", "四", "五", "六", "七", "八", "九" };
        StringBuffer sb = new StringBuffer();
        for (char c : str.toCharArray()) {
            sb.append(s2[Integer.parseInt(String.valueOf(c))]);
        }
        return sb.toString();
    }

}


 

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

百度没有我的爱

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值