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




 

原创粉丝点击