Java 实现中文汉字转拼音

一、说明

很多时候我们都会需要把汉字转化成拼音,方便我们使用。举个栗子:登录场景,客户提供的是一个中文名字,要做成登录,可是用中文名字来登录总是不太好,而且很容易造成乱码的情况出现。所以我们需要把中文登录名转成英文登录名。比如:“张三”需要装成“zhangsan”。下面博主就来使用java编写一个工具类来实现这个需求。

二、效果图

啥也不多说,先直接上效果图。

如果这是你要的效果,那么你可以继续往下看了。

三、准备工作

下载jar,本功能实现用到了一个jar,pinyin4j。大家可以直接 点击下载 ,或者使用下面的pom文件。

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

四、实现代码

package top.zywork.service;

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 Wjhsmart
 */
public class StringUtilsTest {
    /**
     * 测试main方法
     * @param args
     */
    public static void main(String[] args) {
        System.out.println("‘张三’首字母大写:" + toFirstChar("张三").toUpperCase()); //转为首字母大写
        System.out.println("‘张三’转成拼音:" + toPinyin("张三"));
    }
    /**
     * 获取字符串拼音的第一个字母
     * @param chinese
     * @return
     */
    public static String toFirstChar(String chinese){
        String pinyinStr = "";
        char[] newChar = chinese.toCharArray();  //转为单个字符
        HanyuPinyinOutputFormat defaultFormat = new HanyuPinyinOutputFormat();
        defaultFormat.setCaseType(HanyuPinyinCaseType.LOWERCASE);
        defaultFormat.setToneType(HanyuPinyinToneType.WITHOUT_TONE);
        for (int i = 0; i < newChar.length; i++) {
            if (newChar[i] > 128) {
                try {
                    pinyinStr += PinyinHelper.toHanyuPinyinStringArray(newChar[i], defaultFormat)[0].charAt(0);
                } catch (BadHanyuPinyinOutputFormatCombination e) {
                    e.printStackTrace();
                }
            }else{
                pinyinStr += newChar[i];
            }
        }
        return pinyinStr;
    }

    /**
     * 汉字转为拼音
     * @param chinese
     * @return
     */
    public static String toPinyin(String chinese){
        String pinyinStr = "";
        char[] newChar = chinese.toCharArray();
        HanyuPinyinOutputFormat defaultFormat = new HanyuPinyinOutputFormat();
        defaultFormat.setCaseType(HanyuPinyinCaseType.LOWERCASE);
        defaultFormat.setToneType(HanyuPinyinToneType.WITHOUT_TONE);
        for (int i = 0; i < newChar.length; i++) {
            if (newChar[i] > 128) {
                try {
                    pinyinStr += PinyinHelper.toHanyuPinyinStringArray(newChar[i], defaultFormat)[0];
                } catch (BadHanyuPinyinOutputFormatCombination e) {
                    e.printStackTrace();
                }
            }else{
                pinyinStr += newChar[i];
            }
        }
        return pinyinStr;
    }
}

五、写在最后

这2个方法可以自己封装成工具类,然后直接使用。如果你觉得本文对你有帮助,动动手指帮忙点个赞可好?

发布了68 篇原创文章 · 获赞 48 · 访问量 8万+

猜你喜欢

转载自blog.csdn.net/Wjhsmart/article/details/105682332