字符串 汉字转拼音 pinyin4j
Posted baiqiantao
tags:
篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了字符串 汉字转拼音 pinyin4j相关的知识,希望对你有一定的参考价值。
介绍
GitHub上的一个封装:https://github.com/belerweb/pinyin4j
pinyin4J 是一个可以将汉字转换成拼音的lib,非常实用,其提供的 PinyinHelper 这个静态类对外提供拼音转换的服务,主要用到以下两个方法:
static public String[] toHanyuPinyinStringArray(char ch) //将char(必须为汉字单字)转化为拼音,如果ch为非汉字,返回null
static public String[] toHanyuPinyinStringArray(char ch,HanyuPinyinOutputFormat outputFormat) //可以设置输出的格式
1
static public String[] toHanyuPinyinStringArray(char ch) //将char(必须为汉字单字)转化为拼音,如果ch为非汉字,返回null
2
static public String[] toHanyuPinyinStringArray(char ch,HanyuPinyinOutputFormat outputFormat) //可以设置输出的格式
测试
将字符转为拼音数组
char c=‘乾‘;
System.out.println(Arrays.toString(PinyinHelper.toHanyuPinyinStringArray(c)));//[qian2, gan1]
System.out.println(Arrays.toString(PinyinHelper.toGwoyeuRomatzyhStringArray(c)));//[chyan, gan]
System.out.println(Arrays.toString(PinyinHelper.toMPS2PinyinStringArray(c)));//[chian2, gan1]
System.out.println(Arrays.toString(PinyinHelper.toTongyongPinyinStringArray(c)));//[cian2, gan1]
System.out.println(Arrays.toString(PinyinHelper.toWadeGilesPinyinStringArray(c)));//[ch`ien2, kan1]
System.out.println(Arrays.toString(PinyinHelper.toYalePinyinStringArray(c)));//[chyan2, gan1]
x
1
char c=‘乾‘;
2
System.out.println(Arrays.toString(PinyinHelper.toHanyuPinyinStringArray(c)));//[qian2, gan1]
3
System.out.println(Arrays.toString(PinyinHelper.toGwoyeuRomatzyhStringArray(c)));//[chyan, gan]
4
System.out.println(Arrays.toString(PinyinHelper.toMPS2PinyinStringArray(c)));//[chian2, gan1]
5
System.out.println(Arrays.toString(PinyinHelper.toTongyongPinyinStringArray(c)));//[cian2, gan1]
6
System.out.println(Arrays.toString(PinyinHelper.toWadeGilesPinyinStringArray(c)));//[ch`ien2, kan1]
7
System.out.println(Arrays.toString(PinyinHelper.toYalePinyinStringArray(c)));//[chyan2, gan1]
对于 乾 和 重 的拼音转换,以上方法分别得到的结果是:
汉语拼音:[qian2, gan1],[zhong4, chong2]
国语罗马字:[chyan, gan],[jonq, chorng]
注音符号拼音:[chian2, gan1],[jung4, chung2]
通用拼音:[cian2, gan1],[jhong4, chong2]
威妥玛拼音:[ch`ien2, kan1],[chung4, ch`ung2]
耶魯拼音:[chyan2, gan1],[jung4, chung2]
x
1
汉语拼音:[qian2, gan1],[zhong4, chong2]
2
国语罗马字:[chyan, gan],[jonq, chorng]
3
注音符号拼音:[chian2, gan1],[jung4, chung2]
4
通用拼音:[cian2, gan1],[jhong4, chong2]
5
威妥玛拼音:[ch`ien2, kan1],[chung4, ch`ung2]
6
耶魯拼音:[chyan2, gan1],[jung4, chung2]
拼音输出格式设置
HanyuPinyinOutputFormat format = new HanyuPinyinOutputFormat();
System.out.println(Arrays.toString(PinyinHelper.toHanyuPinyinStringArray(c, format)));
x
1
HanyuPinyinOutputFormat format = new HanyuPinyinOutputFormat();
2
System.out.println(Arrays.toString(PinyinHelper.toHanyuPinyinStringArray(c, format)));
设置大小写,默认 LOWERCASE
format.setCaseType(HanyuPinyinCaseType.UPPERCASE);//大小写,默认 LOWERCASE
1
format.setCaseType(HanyuPinyinCaseType.UPPERCASE);//大小写,默认 LOWERCASE
设置音调的样式:数字WITH_TONE_NUMBER,声调符WITH_TONE_MARK,无声调。默认 WITH_TONE_NUMBER
format.setToneType(HanyuPinyinToneType.WITHOUT_TONE);//音调的样式,默认 WITH_TONE_NUMBER
//[QIAN2, GAN1],[QIáN, GāN],[QIAN, GAN]
x
1
format.setToneType(HanyuPinyinToneType.WITHOUT_TONE);//音调的样式,默认 WITH_TONE_NUMBER
2
//[QIAN2, GAN1],[QIáN, GāN],[QIAN, GAN]
符合 V 的输出格式,使用U且带冒号WITH_U_AND_COLON,使用U且上面带雨点WITH_U_UNICODE,使用V符合WITH_V,默认 WITH_U_AND_COLON
format.setVCharType(HanyuPinyinVCharType.WITH_V);//符合 V 的输出格式,默认 WITH_U_AND_COLON
//[LU:, LU],[Lü, LU],[LV, LU]
x
1
format.setVCharType(HanyuPinyinVCharType.WITH_V);//符合 V 的输出格式,默认 WITH_U_AND_COLON
2
//[LU:, LU],[Lü, LU],[LV, LU]
字符串转拼音工具类
System.out.println(toPinYin("白乾涛"));//Bai2Qian2Tao1
1
System.out.println(toPinYin("白乾涛"));//Bai2Qian2Tao1
public static String toPinYin(String content) {
return toPinYin(content, "", true, null);
}
/**
* 将content转换成拼音,如果不是汉字或者没有对应的拼音,则不作转换
* 如: 明天 转换成 MINGTIAN
* @param content:要转化的汉字
* @param spera:转化结果的分割符
* @param firstUpperCase:是否首字母大写
* @param format:格式封装类
*/
public static String toPinYin(String content, String spera, boolean firstUpperCase, HanyuPinyinOutputFormat format) {
if (content == null || content.trim().length() == 0) {
return "";
}
if (spera == null) {
spera = "";
}
if (format == null) {
format = new HanyuPinyinOutputFormat();
}
StringBuilder pyBuilder = new StringBuilder();
String temp = "";
String[] pyArray = null;
for (int i = 0; i < content.length(); i++) {
char c = content.charAt(i);
if ((int) c <= 128) {
pyBuilder.append(c);
} else {
try {
pyArray = PinyinHelper.toHanyuPinyinStringArray(c, format);
} catch (BadHanyuPinyinOutputFormatCombination e) {
e.printStackTrace();
}
if (pyArray == null) {
pyBuilder.append(c);
} else {
temp = pyArray[0];
if (firstUpperCase) {
temp = pyArray[0].toUpperCase().charAt(0) + temp.substring(1);
}
pyBuilder.append(temp + (i == content.length() - 1 ? "" : spera));
}
}
}
return pyBuilder.toString().trim();
}
x
1
public static String toPinYin(String content) {
2
return toPinYin(content, "", true, null);
3
}
4
5
/**
6
* 将content转换成拼音,如果不是汉字或者没有对应的拼音,则不作转换
7
* 如: 明天 转换成 MINGTIAN
8
* @param content:要转化的汉字
9
* @param spera:转化结果的分割符
10
* @param firstUpperCase:是否首字母大写
11
* @param format:格式封装类
12
*/
13
public static String toPinYin(String content, String spera, boolean firstUpperCase, HanyuPinyinOutputFormat format) {
14
if (content == null || content.trim().length() == 0) {
15
return "";
16
}
17
if (spera == null) {
18
spera = "";
19
}
20
if (format == null) {
21
format = new HanyuPinyinOutputFormat();
22
}
23
24
StringBuilder pyBuilder = new StringBuilder();
25
String temp = "";
26
String[] pyArray = null;
27
for (int i = 0; i < content.length(); i++) {
28
char c = content.charAt(i);
29
if ((int) c <= 128) {
30
pyBuilder.append(c);
31
} else {
32
try {
33
pyArray = PinyinHelper.toHanyuPinyinStringArray(c, format);
34
} catch (BadHanyuPinyinOutputFormatCombination e) {
35
e.printStackTrace();
36
}
37
if (pyArray == null) {
38
pyBuilder.append(c);
39
} else {
40
temp = pyArray[0];
41
if (firstUpperCase) {
42
temp = pyArray[0].toUpperCase().charAt(0) + temp.substring(1);
43
}
44
pyBuilder.append(temp + (i == content.length() - 1 ? "" : spera));
45
}
46
}
47
}
48
return pyBuilder.toString().trim();
49
}
2018-7-2
以上是关于字符串 汉字转拼音 pinyin4j的主要内容,如果未能解决你的问题,请参考以下文章