Java漢字轉(zhuǎn)成漢語(yǔ)拼音工具類(lèi),需要用到pinyin4j.jar包.

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;
/**
* 漢字轉(zhuǎn)拼音工具類(lèi)
*
* @version 1.00 (2014.09.15)
* @author SHNKCS 張軍 {@link <a target=_blank href=http://www.dlhighland.cn>張軍個(gè)人網(wǎng)站</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("轉(zhuǎn)換拼音出錯(cuò),返回原字符", 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("轉(zhuǎn)換拼音首字母出錯(cuò),返回原字符", 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();
}
}
本文為張軍原創(chuàng)文章,轉(zhuǎn)載無(wú)需和我聯(lián)系,但請(qǐng)注明來(lái)自張軍的軍軍小站,個(gè)人博客http://www.dlhighland.cn
更多文章、技術(shù)交流、商務(wù)合作、聯(lián)系博主
微信掃碼或搜索:z360901061
微信掃一掃加我為好友
QQ號(hào)聯(lián)系: 360901061
您的支持是博主寫(xiě)作最大的動(dòng)力,如果您喜歡我的文章,感覺(jué)我的文章對(duì)您有幫助,請(qǐng)用微信掃描下面二維碼支持博主2元、5元、10元、20元等您想捐的金額吧,狠狠點(diǎn)擊下面給點(diǎn)支持吧,站長(zhǎng)非常感激您!手機(jī)微信長(zhǎng)按不能支付解決辦法:請(qǐng)將微信支付二維碼保存到相冊(cè),切換到微信,然后點(diǎn)擊微信右上角掃一掃功能,選擇支付二維碼完成支付。
【本文對(duì)您有幫助就好】元

