首先去官网下在jar包,pinyin4j-2.5.0.jar
package com.his.util;
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 HanZiToPinYin {
public static String toPinYin(String str) {
String py = "";
String[] t = new String[str.length()];
char [] hanzi=new char[str.length()];
for(int i=0;i<str.length();i++){
hanzi[i]=str.charAt(i);
}
net.sourceforge.pinyin4j.format.HanyuPinyinOutputFormat t1 = new HanyuPinyinOutputFormat();
t1.setCaseType(HanyuPinyinCaseType.LOWERCASE);
t1.setToneType(HanyuPinyinToneType.WITHOUT_TONE);
t1.setVCharType(HanyuPinyinVCharType.WITH_V);
try {
for (int i = 0; i < str.length(); i++) {
if ((str.charAt(i) >= 'a' && str.charAt(i) < 'z')
|| (str.charAt(i) >= 'A' && str.charAt(i) <= 'Z')
|| (str.charAt(i) >= '0' && str.charAt(i) <= '9')) {
py += str.charAt(i);
} else {
t = PinyinHelper.toHanyuPinyinStringArray(hanzi[i], t1);
py=py+t[0];
}
}
} catch (BadHanyuPinyinOutputFormatCombination e) {
e.printStackTrace();
}
return py.trim().toString();
}
public static void main(String args[]){
System.out.println(HanZiToPinYin.toPinYin("汉字转拼音"));
}
}