1.需要引入的jar包:pinyin4j-2.5.0.jar
2.code如下:
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 PinyinToolkit {
public static void main(String[] args) throws UnsupportedEncodingException {
String x = "测试测试";
System.out.println(getPinYin(x));
System.out.println(cnFirstSpell(x));
System.out.println(cnFullSpell(x));
}
/**
* 获取汉字串拼音首字母,英文字符不变
*
* @param chinese 汉字串
* @return 汉语拼音首字母
*/
public static String cnFirstSpell(String chinese) {
StringBuffer pybf = new StringBuffer();
char[] arr = chinese.toCharArray();
HanyuPinyinOutputFormat defaultFormat = new HanyuPinyinOutputFormat();
defaultFormat.setCaseType(HanyuPinyinCaseType.LOWERCASE);
defaultFormat.setToneType(HanyuPinyinToneType.WITHOUT_TONE);
for (int i = 0; i < arr.length; i++) {
if (arr[i] > 128) {
try {
String[] _t = PinyinHelper.toHanyuPinyinStringArray(arr[i], defaultFormat);
if (_t != null) {
pybf.append(_t[0].charAt(0));
}
} catch (BadHanyuPinyinOutputFormatCombination e) {
e.printStackTrace();
}
} else {
pybf.append(arr[i]);
}
}
return pybf.toString().replaceAll("\\W", "").trim();
}
/**
* 获取汉字串拼音,英文字符不变
* @param chinese 汉字串
* @return 汉语拼音
*/
public static String cnFullSpell(String chinese) {
StringBuffer pybf = new StringBuffer();
char[] arr = chinese.toCharArray();
HanyuPinyinOutputFormat defaultFormat = new HanyuPinyinOutputFormat();
defaultFormat.setCaseType(HanyuPinyinCaseType.LOWERCASE);
defaultFormat.setVCharType(HanyuPinyinVCharType.WITH_V);
defaultFormat.setToneType(HanyuPinyinToneType.WITH_TONE_NUMBER);
for (int i = 0; i < arr.length; i++) {
if (arr[i] > 128) {
try {
pybf.append(PinyinHelper.toHanyuPinyinStringArray(arr[i], defaultFormat)[0]);
} catch (BadHanyuPinyinOutputFormatCombination e) {
e.printStackTrace();
}
} else {
pybf.append(arr[i]);
}
}
return pybf.toString();
}
/**
* 方法描述:<b>获取汉字串拼音,英文字符不变</b></br>
* 备 注: </br>
* @param chinese
* @return 汉语拼音没有音调
*/
public static String getPinYin(String chinese) {
char[] chars = null;
chars = chinese.toCharArray();
String[] strLength = new String[chars.length];
// 设置汉字拼音输出的格式
HanyuPinyinOutputFormat hanyupingyin = new HanyuPinyinOutputFormat();
hanyupingyin.setCaseType(HanyuPinyinCaseType.LOWERCASE);
hanyupingyin.setToneType(HanyuPinyinToneType.WITHOUT_TONE);
hanyupingyin.setVCharType(HanyuPinyinVCharType.WITH_V);
String strResult = "";
int intLength = chars.length;
try {
for (int i = 0; i < intLength; i++) {
// 判断是否为汉字字符
if (Character.toString(chars[i]).matches("[\\u4E00-\\u9FA5]+")) {
strLength = PinyinHelper.toHanyuPinyinStringArray(chars[i], hanyupingyin);// 将汉字的几种全拼都存到t2数组中
strResult += strLength[0];// 取出该汉字全拼的第一种读音并连接到字符串t4后
} else {
// 如果不是汉字字符,直接取出字符并连接到字符串t4后
strResult += Character.toString(chars[i]);
}
}
} catch (BadHanyuPinyinOutputFormatCombination e) {
}
return strResult;
}
}