标签:java
需要java包 :pinyin4j-2.5.0.jar
pinyin4j的使用很方便,一般转换只需要使用PinyinHelper类的静态工具方法即可:
package com.achni.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 SpellHelper { //将中文转换为英文 public static String getEname(String name) throws BadHanyuPinyinOutputFormatCombination { //首先需要创建格式化对象HanyuPinyinOutputFormat HanyuPinyinOutputFormat pyFormat = new HanyuPinyinOutputFormat(); //设置大小写格式 //outputFormat.setCaseType(HanyuPinyinCaseType); //HanyuPinyinCaseType.LOWERCASE 转换后以全小写方式输出 //HanyuPinyinCaseType.UPPERCASE 转换后以全大写方式输出 pyFormat.setCaseType(HanyuPinyinCaseType.LOWERCASE); //方法参数HanyuPinyinToneType有以下常量对象: //HanyuPinyinToneType.WITH_TONE_NUMBER 用数字表示声调,例如:zhao4 //HanyuPinyinToneType.WITHOUT_TONE 无声调表示,例如:zhao //HanyuPinyinToneType.WITH_TONE_MARK 用声调符号表示,例如:zhao pyFormat.setToneType(HanyuPinyinToneType.WITHOUT_TONE); //方法参数HanyuPinyinVCharType有以下常量对象: //HanyuPinyinVCharType.WITH_U_AND_COLON 以U和一个冒号表示该拼音, //HanyuPinyinVCharType.WITH_V 以V表示该字符, //HanyuPinyinVCharType.WITH_U_UNICODE pyFormat.setVCharType(HanyuPinyinVCharType.WITH_V); return PinyinHelper.toHanyuPinyinString(name, pyFormat,""); } public static void main(String[] args) { try { System.out.println(getEname("梅艳芳")); } catch (BadHanyuPinyinOutputFormatCombination e) { // TODO Auto-generated catch block e.printStackTrace(); } } }
标签:java
原文地址:http://blog.csdn.net/blogluoqi/article/details/30248893