Markdown版本笔记 | 我的GitHub首页 | 我的博客 | 我的微信 | 我的邮箱 |
---|---|---|---|---|
MyAndroidBlogs | baiqiantao | baiqiantao | bqt20094 | baiqiantao@sina.com |
目录
介绍
pinyin4J 是一个可以将汉字转换成拼音的lib,非常实用,其提供的 PinyinHelper 这个静态类对外提供拼音转换的服务,主要用到以下两个方法:
static public String[] toHanyuPinyinStringArray(char ch) //将char(必须为汉字单字)转化为拼音,如果ch为非汉字,返回null
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]
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]
对于 乾 和 重 的拼音转换,以上方法分别得到的结果是:
汉语拼音:[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]
拼音输出格式设置
HanyuPinyinOutputFormat format = new HanyuPinyinOutputFormat();
System.out.println(Arrays.toString(PinyinHelper.toHanyuPinyinStringArray(c, format)));
设置大小写,默认 LOWERCASE
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]
符合 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]
字符串转拼音工具类
System.out.println(PinYinUtils.toPinYin("白乾涛"));//Bai2Qian2Tao1
System.out.println(PinYinUtils.getFirstPinYin("白乾涛"));//B
public class PinYinUtils {
//获取字符串首个字符转为拼音后的首个字母的大写形式
public static String getFirstPinYin(String content) {
if (content == null || content.trim().length() == 0) {
return "#";
} else {
char firstChar = content.trim().charAt(0);//取字符串首个字符
if (firstChar >= 'A' && firstChar <= 'Z') { //如果是大写字母,直接返回
return firstChar + "";
} else if (firstChar >= 'a' && firstChar <= 'z') { //如果是小写字母,转为大写后返回
return (firstChar + "").toUpperCase();
} else {
String[] array = PinyinHelper.toHanyuPinyinStringArray(firstChar);
return array == null || array.length == 0 || array[0] == null || array[0].length() == 0 ? "#" : (array[0].charAt(0) + "").toUpperCase();
}
}
}
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).append(i == content.length() - 1 ? "" : spera);
}
}
}
return pyBuilder.toString().trim();
}
}
2018-7-2