java汉字转换拼音
来源:互联网 发布:动感照片制作软件 编辑:程序博客网 时间:2024/04/28 12:38
1、汉字转换拼音首先引入一个叫pinyin4j-2.5.0.jar
下载地址:http://download.csdn.net/detail/yao__shun__yu/4670228
2、测试代码
import demo.Pinyin4jAppletDemo;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 T {public static void main(String[] args) throws BadHanyuPinyinOutputFormatCombination {char[] ch = "平淡".toCharArray();HanyuPinyinOutputFormat t3 = new HanyuPinyinOutputFormat();/** * 拼音大小写类型 *///t3.setCaseType(HanyuPinyinCaseType.LOWERCASE);//小写拼音//t3.setCaseType(HanyuPinyinCaseType.UPPERCASE);//大写拼音/** * 拼音输出方式 *///t3.setToneType(HanyuPinyinToneType.WITHOUT_TONE);//不输出语气,就是几声t3.setToneType(HanyuPinyinToneType.WITH_TONE_NUMBER);//数字方式输出几声//t3.setToneType(HanyuPinyinToneType.WITH_TONE_MARK);//符号方式的输出几声/** * 拼音字母样式 *///The option indicates that the output of 'ü' is "v"//t3.setVCharType(HanyuPinyinVCharType.WITH_V);//The option indicates that the output of 'ü' is "ü" in Unicode form//t3.setVCharType(HanyuPinyinVCharType.WITH_U_AND_COLON);//The option indicates that the output of 'ü' is "u:"//t3.setVCharType(HanyuPinyinVCharType.WITH_U_UNICODE);System.out.println(ch);//不加样式String[] pinyinHead = PinyinHelper.toHanyuPinyinStringArray(ch[0]);for(String str:pinyinHead){System.out.println(str);}//加样式pinyinHead = PinyinHelper.toHanyuPinyinStringArray(ch[1],t3);for(String str:pinyinHead){System.out.println(str);}}}
运行结果:
平淡ping2dan4
标准案例:
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 ChineseToEnglish{// ------------------------灏嗘眽瀛楄浆鎹负鍏ㄦ嫾-------------------------------public static String getPingYin(String src){char[] t1 = null;t1 = src.toCharArray();String[] t2 = new String[t1.length];HanyuPinyinOutputFormat t3 = new HanyuPinyinOutputFormat();t3.setCaseType(HanyuPinyinCaseType.LOWERCASE);t3.setToneType(HanyuPinyinToneType.WITHOUT_TONE);t3.setVCharType(HanyuPinyinVCharType.WITH_V);String t4 = "";int t0 = t1.length;try{for (int i = 0; i < t0; i++){// 鍒ゆ柇鏄惁涓烘眽瀛楀瓧绗�if (java.lang.Character.toString(t1[i]).matches("[\\u4E00-\\u9FA5]+")){t2 = PinyinHelper.toHanyuPinyinStringArray(t1[i], t3);t4 += t2[0];} else{t4 += java.lang.Character.toString(t1[i]);}}// System.out.println(t4);return t4;} catch (BadHanyuPinyinOutputFormatCombination e1){e1.printStackTrace();}return t4;}// ----------------------------------------杩斿洖涓枃鐨勯瀛楁瘝--------------------public static String getPinYinHeadChar(String str){String convert = "";for (int j = 0; j < str.length(); j++){char word = str.charAt(j);String[] pinyinArray = PinyinHelper.toHanyuPinyinStringArray(word);if (pinyinArray != null){convert += pinyinArray[0].charAt(0);} else{convert += word;}}return convert;}// -------------------------灏嗗瓧绗︿覆杞Щ涓篈SCII鐮�----------------------------public static String getCnASCII(String cnStr){StringBuffer strBuf = new StringBuffer();byte[] bGBK= cnStr.getBytes();for (int i = 0; i < bGBK.length; i++){// System.out.println(Integer.toHexString(bGBK[i]&0xff));strBuf.append(Integer.toHexString(bGBK[i] & 0xff));}return strBuf.toString();}// --------------------------娴嬭瘯鎷奸煶杞眽瀛楁槸鍚︽纭�---------------------------public static void main(String[] args){System.out.println(getPingYin("东软帝国"));// 杩斿洖鎷奸煶鐨勫叏鎷兼牱寮�System.out.println(getPinYinHeadChar("东软帝国"));// 杩斿洖鎷奸煶鐨勯瀛楁瘝System.out.println(getCnASCII("东软帝国"));// 杩斿洖鎷奸煶鐨凙SCII}}
运行结果
dongruandiguodrdge4b89ce8bdafe5b89de59bbd
- java汉字转换拼音
- java拼音汉字转换
- java 汉字转换成拼音
- java 汉字转换成拼音
- Java 汉字转换成拼音
- Java汉字转换为拼音
- Java汉字转换为拼音
- java汉字转换为拼音
- java-汉字转换拼音工具
- Java汉字转换为拼音
- Java汉字转换为拼音
- Java汉字转换为拼音
- java 汉字转换成拼音
- Java汉字转换为拼音
- java汉字转换拼音,获取汉字串拼音首字母
- Java实现汉字转换为拼音
- Java实现汉字转换为拼音
- Java实现汉字转换为拼音
- 一步一步学习linux--网络设置篇
- Android SDK下载和更新失败的解决方法
- c++ 编程题2
- MCS-51之串口通信
- 适配器模式--Adapter
- java汉字转换拼音
- Spring MVC发送邮件
- 打开OrCAD capture出现runtime error的解决方法
- Spring发送html邮件
- 归并排序
- 字符加密
- 磁力链接
- sping mvc mail
- Mysql Blob类型大小 上传文件大小问题