本文实例讲述了Java实现的汉语拼音工具类。分享给大家供大家参考,具体如下:package test;import net.sourceforge.pinyin4j.PinyinHelper;import net.sourceforge.p
本文实例讲述了Java实现的汉语拼音工具类。分享给大家供大家参考,具体如下:
package test;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 PingYinUtil { public static String getAllLeter(String args) { String result = ""; char[] charArray = args.toCharArray(); String[] strArr = new String[charArray.length]; HanyuPinyinOutputFormat format = new HanyuPinyinOutputFormat(); format.setCaseType(HanyuPinyinCaseType.LOWERCASE); format.setToneType(HanyuPinyinToneType.WITHOUT_TONE); format.setVCharType(HanyuPinyinVCharType.WITH_V); int len = charArray.length; for (int i = 0; i <len ; i++) { try { strArr = PinyinHelper.toHanyuPinyinStringArray(charArray[i], format); if (strArr == null){ result += charArray[i]; }else { result += strArr[0]; } } catch (BadHanyuPinyinOutputFormatCombination e) { e.printStackTrace(); } } return result; } public static String getFirstLeter(String args) { String result = ""; char[] charArray = args.toCharArray(); String[] strArr = new String[charArray.length]; HanyuPinyinOutputFormat format = new HanyuPinyinOutputFormat(); format.setCaseType(HanyuPinyinCaseType.LOWERCASE); format.setToneType(HanyuPinyinToneType.WITHOUT_TONE); format.setVCharType(HanyuPinyinVCharType.WITH_V); int len = charArray.length; for (int i = 0; i <len ; i++) { try { strArr = PinyinHelper.toHanyuPinyinStringArray(charArray[i], format); if (strArr == null){ result += charArray[i]; }else { result += strArr[0].substring(0,1); } } catch (BadHanyuPinyinOutputFormatCombination e) { e.printStackTrace(); } } return result; } public static void main(String[] args) { System.out.println("编程网测试结果:"); System.out.println("getAllLeter==" + getAllLeter("你好啊")); System.out.println("getFirstLeter==" + getFirstLeter("你好啊")); }}
--结束END--
本文标题: Java实现的汉语拼音工具类完整实例
本文链接: https://lsjlt.com/news/221849.html(转载时请注明来源链接)
有问题或投稿请发送至: 邮箱/279061341@qq.com QQ/279061341
2024-05-24
2024-05-24
2024-05-24
2024-05-24
2024-05-24
2024-05-24
2024-05-24
2024-05-24
2024-05-24
2024-05-24
回答
回答
回答
回答
回答
回答
回答
回答
回答
回答
0