最近在工作开发中有一个需求,需要将用户的中文名转换为拼音。经过一番搜索之后,我找到了实现方法。
首先引入第三方jar包pinyin4j-2.5.0.jar
,对应的maven依赖如下:
<dependency>
<groupId>com.github.open-android</groupId>
<artifactId>pinyin4j</artifactId>
<version>2.5.0</version>
</dependency>
具体实现代码如下:
package com.wunian.pinyin;
import net.sourceforge.pinyin4j.PinyinHelper;
import net.sourceforge.pinyin4j.format.HanyuPinyinOutputFormat;
import net.sourceforge.pinyin4j.format.HanyuPinyinToneType;
import java.util.Scanner;
import java.util.regex.Matcher;
import java.util.regex.Pattern;
/**
* @author wunian
* @desc 汉字转拼音
* @createTime 2020/7/16
*/
public class HanZi2PinYinDemo {
public static void main(String[] args) {
// TODO Auto-generated method stub
Scanner scanner =new Scanner(System.in);
System.out.println("请输入汉字:");
String hanzi = scanner.next();
System.out.println("转换成拼音:");
System.out.println(convertHanzi2Pinyin(hanzi, true));
}
public static String convertHanzi2Pinyin(String hanzi, boolean full) {
/***
* ^[\u2E80-\u9FFF]+$ 匹配所有东亚区的语言 ^[\u4E00-\u9FFF]+$ 匹配简体和繁体
* ^[\u4E00-\u9FA5]+$ 匹配简体
*/
String regExp = "^[\u4E00-\u9FFF]+$";
StringBuffer sb = new StringBuffer();
if (hanzi == null || "".equals(hanzi.trim())) {
return "";
}
String pinyin = "";
for (int i = 0; i < hanzi.length(); i++) {
char unit = hanzi.charAt(i);
if (match(String.valueOf(unit), regExp))// 是汉字,则转拼音
{
pinyin = convertSingleHanzi2Pinyin(unit);
if (full) {
sb.append(pinyin);
} else {
sb.append(pinyin.charAt(0));
}
} else {
sb.append(unit);
}
}
return sb.toString();
}
/***
* 将单个汉字转成拼音
*
* @param hanzi
* @return
*/
private static String convertSingleHanzi2Pinyin(char hanzi) {
HanyuPinyinOutputFormat outputFormat = new HanyuPinyinOutputFormat();
outputFormat.setToneType(HanyuPinyinToneType.WITHOUT_TONE);
String[] res;
StringBuffer sb = new StringBuffer();
try {
res = PinyinHelper.toHanyuPinyinStringArray(hanzi, outputFormat);
sb.append(res[0]);// 对于多音字,只用第一个拼音
} catch (Exception e) {
e.printStackTrace();
return "";
}
return sb.toString();
}
/***
* @param str
* 源字符串
* @param regex
* 正则表达式
* @return 是否匹配
*/
public static boolean match(String str, String regex) {
Pattern pattern = Pattern.compile(regex);
Matcher matcher = pattern.matcher(str);
return matcher.find();
}
}