需要jar包:pinyin4j-5.5.0.jar
工具类:ChinesePinYin
链接:https://pan.baidu.com/s/1gfvHCXHYZCP3Zpiy0-LM6Q?pwd=zxcv
提取码:zxcv
工具类:
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 ChinesePinYin {
public static String getPinYin(String inputString) {
HanyuPinyinOutputFormat format = new HanyuPinyinOutputFormat();
format.setCaseType(HanyuPinyinCaseType.LOWERCASE);
format.setToneType(HanyuPinyinToneType.WITHOUT_TONE);
format.setVCharType(HanyuPinyinVCharType.WITH_V);
char[] input = inputString.trim().toCharArray();
String output = "";
try {
for (int i = 0; i < input.length; i++) {
if (Character.toString(input[i]).matches("[\\u4E00-\\u9FA5]+")) { //判断字符是否是中文
//toHanyuPinyinStringArray 如果传入的不是汉字,就不能转换成拼音,那么直接返回null
//由于中文有很多是多音字,所以这些字会有多个String,在这里我们默认的选择第一个作为pinyin
String[] temp = PinyinHelper.toHanyuPinyinStringArray(input[i], format);
output += temp[0];
} else {
output += Character.toString(input[i]);
}
}
} catch (BadHanyuPinyinOutputFormatCombination e) {
e.printStackTrace();
// Log.v(TAG, "BadHanyuPinyinOutputFormatCombination");
}
return output;
}
/**
* 取出拼音中第一个字母,一般第一个字母的使用时比较常见的
* @param chines
* @return
*/
public static String converterToFirstSpell(String chines) {
String pinyinName = "";
char[] nameChar = chines.toCharArray();
HanyuPinyinOutputFormat defaulFormat = new HanyuPinyinOutputFormat();
defaulFormat.setCaseType(HanyuPinyinCaseType.LOWERCASE);
defaulFormat.setToneType(HanyuPinyinToneType.WITHOUT_TONE);
defaulFormat.setVCharType(HanyuPinyinVCharType.WITH_V);
for (int i = 0; i < nameChar.length; i++) {
if (nameChar[i] > 128) {
try {
pinyinName += PinyinHelper.toHanyuPinyinStringArray(nameChar[i], defaulFormat)[0].charAt(0);
} catch (BadHanyuPinyinOutputFormatCombination ex) {
ex.printStackTrace();
}
} else {
pinyinName += nameChar[i];
}
}
return pinyinName;
}
}
main方法测试
public static void main(String[] args) {
String str=ChinesePinYin.getPinYin("我爱南京");
System.out.println(str);
}
woainanjing
Process finished with exit code 0