Java漢字轉成漢語拼音工具類,需要用到pinyin4j.jar包.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 | package zj.pinyin; 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 org.apache.log4j.Logger; /** * 漢字轉拼音工具類 * * @version 1.00 (2014.09.15) * @author SHNKCS 張軍 {@link <a target=_blank href=http://m.eyofj.com>張軍個人網站</a> <a target=_blank href=http://user.qzone.qq.com/360901061/>張軍QQ空間</a>} */ public class PinyinUtil { private transient static final Logger logger = Logger.getLogger(PinyinUtil. class ); /** * 拼音字母 * * @param text * 原字符 * @author 張軍 * @date 2016-2-1 9:16:00 * @modifiyNote * @version 1.0 * @return 拼音首字母 */ public static String textToPingYin(String text) { try { char [] t1 = null ; t1 = text.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); int t0 = t1.length; String t4 = "" ; 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]); } } return t4; } catch (Exception e) { logger.debug( "轉換拼音出錯,返回原字符" , e); return text; } } /** * 拼音首字母 * * @param text * 原字符 * @author 張軍 * @date 2016-2-1 9:16:00 * @modifiyNote * @version 1.0 * @return 拼音首字母 */ public static String textToPinYinHeadChar(String text) { try { String convert = "" ; for ( int j = 0 ; j < text.length(); j++) { char word = text.charAt(j); String[] pinyinArray = null ; if (java.lang.Character.toString(word).matches( "[\\u4E00-\\u9FA5]+" )) { pinyinArray = PinyinHelper.toHanyuPinyinStringArray(word, new HanyuPinyinOutputFormat()); } if (pinyinArray == null ) { convert += word; } else { convert += pinyinArray[ 0 ].charAt( 0 ); } } return convert; } catch (Exception e) { logger.debug( "轉換拼音首字母出錯,返回原字符" , e); return text; } } /** * 獲取ascii碼 * * @param text * 原字符 * @author 張軍 * @date 2016-2-1 9:16:00 * @modifiyNote * @version 1.0 * @return ascii碼 */ 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(); } } |
本文為張軍原創文章,轉載無需和我聯系,但請注明來自張軍的軍軍小站,個人博客http://m.eyofj.com
更多文章、技術交流、商務合作、聯系博主
微信掃碼或搜索:z360901061

微信掃一掃加我為好友
QQ號聯系: 360901061
您的支持是博主寫作最大的動力,如果您喜歡我的文章,感覺我的文章對您有幫助,請用微信掃描下面二維碼支持博主2元、5元、10元、20元等您想捐的金額吧,狠狠點擊下面給點支持吧,站長非常感激您!手機微信長按不能支付解決辦法:請將微信支付二維碼保存到相冊,切換到微信,然后點擊微信右上角掃一掃功能,選擇支付二維碼完成支付。
【本文對您有幫助就好】元
