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 Test { //用于存放输出样式的 private HanyuPinyinOutputFormat outputFormat = new HanyuPinyinOutputFormat(); public Test() { outputFormat.setCaseType(HanyuPinyinCaseType.LOWERCASE);//大小写的问题 outputFormat.setVCharType(HanyuPinyinVCharType.WITH_U_AND_COLON);//关于v的显示方式 outputFormat.setToneType(HanyuPinyinToneType.WITHOUT_TONE);//关于音标的显示 } public static void main(String args[]) throws BadHanyuPinyinOutputFormatCombination { Test test = new Test(); String result = test.convertToPinYin("中文aaaaabbb.....',;12.3"); System.out.println(result); } public String convertToPinYin(String str) throws BadHanyuPinyinOutputFormatCombination { char[] chars = str.toCharArray(); StringBuffer result = new StringBuffer(); for (char c:chars) { if (Character.toString(c).matches("[\\u4E00-\\u9FA5]+")) {//表示匹配中文 //下面的results是表示该中文字所有的拼音,如“中”有"中文”、"相中"两种读法,所以当只需要拼音而不需要知道读第几声时,只需取第一个,多音字需慎重 String results[] = PinyinHelper.toHanyuPinyinStringArray(c, outputFormat); result.append(results[0]); //字母和数字直接显示 } else if (Character.isLetter(c)|| Character.isDigit(c)){ result.append(c); } } return result.toString(); } }