博客第一篇日志啊,呵呵,那就記錄一下我昨天在工作中遇到的漢字轉(zhuǎn)換拼音的問(wèn)題吧~~
在工程中引入pinyin4j-2.5.0.jar,該jar包可以實(shí)現(xiàn)漢字轉(zhuǎn)換為拼音,漢字轉(zhuǎn)換為拼音首字母,以及得到字符串ASCII碼,具體用法參見(jiàn)以下代碼:
package 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.exception.BadHanyuPinyinOutputFormatCombination;
/**
* 漢字轉(zhuǎn)換位漢語(yǔ)拼音,英文字符不變
*
* @author
*
*/
public class CnToSpell {
/**
* 漢字轉(zhuǎn)換為漢語(yǔ)拼音首字母,英文字符不變
*
* @param chines
* 漢字
* @return 拼音
*/
public static String getFirstSpell(String chines) {
String pinyinName = "";
char[] nameChar = chines.toCharArray();
HanyuPinyinOutputFormat defaultFormat = new HanyuPinyinOutputFormat();
defaultFormat.setCaseType(HanyuPinyinCaseType.LOWERCASE);
defaultFormat.setToneType(HanyuPinyinToneType.WITHOUT_TONE);
for (int i = 0; i < nameChar.length; i++) {
if (nameChar[i] > 128) {
try {
pinyinName += PinyinHelper.toHanyuPinyinStringArray(
nameChar[i], defaultFormat)[0].charAt(0);
} catch (BadHanyuPinyinOutputFormatCombination e) {
e.printStackTrace();
} catch (NullPointerException e) {
// 如果是日文,可能拋出該異常
e.printStackTrace();
}
} else {
pinyinName += nameChar[i];
}
}
return pinyinName;
}
/**
* 漢字轉(zhuǎn)換位漢語(yǔ)拼音,英文字符不變
*
* @param chines
* 漢字
* @return 拼音
*/
public static String getFullSpell(String chines) {
String pinyinName = "";
char[] nameChar = chines.toCharArray();
HanyuPinyinOutputFormat defaultFormat = new HanyuPinyinOutputFormat();
defaultFormat.setCaseType(HanyuPinyinCaseType.LOWERCASE);
defaultFormat.setToneType(HanyuPinyinToneType.WITHOUT_TONE);
for (int i = 0; i < nameChar.length; i++) {
if (nameChar[i] > 128) {
try {
pinyinName += PinyinHelper.toHanyuPinyinStringArray(
nameChar[i], defaultFormat)[0];
} catch (BadHanyuPinyinOutputFormatCombination e) {
e.printStackTrace();
} catch (NullPointerException e) {
// 如果是日文,可能拋出該異常
e.printStackTrace();
}
} else {
pinyinName += nameChar[i];
}
}
return pinyinName;
}
/**
* 將字符串轉(zhuǎn)換成ASCII碼
*
* @param cnStr
* @return String
*/
public static String getCnASCII(String chines) {
StringBuffer strBuf = new StringBuffer();
// 將字符串轉(zhuǎn)換成字節(jié)序列
byte[] bGBK = chines.getBytes();
for (int i = 0; i < bGBK.length; i++) {
// System.out.println(Integer.toHexString(bGBK[i] & 0xff));
// 將每個(gè)字符轉(zhuǎn)換成ASCII碼
strBuf.append(Integer.toHexString(bGBK[i] & 0xff));
}
return strBuf.toString();
}
public static void main(String[] args) {
System.out.println(getFirstSpell("陶喆"));
System.out.println(getFullSpell("陶喆"));
System.out.println(getFirstSpell("歡迎來(lái)到Java中文社區(qū)"));
// System.out.println(getFullSpell("にほん歡/.,;'迎來(lái)到Java中文社區(qū)"));// 日文會(huì)報(bào)異常哦
System.out.println(getFullSpell("嘅囧誰(shuí)說(shuō)壞學(xué)生來(lái)勼髮視頻襠児"));
System.out.println(getCnASCII("嘅囧"));
}
}
?
輸出結(jié)果為:
tz
taozhe
hyldJavazwsq
kaijiongshuishuohuaixueshenglaijiufashipindanger
86fe87e5
更多文章、技術(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ì)您有幫助就好】元

