您的位置:首页 > 编程语言 > Java开发

java汉字转换为拼音首字母

2014-12-07 11:33 309 查看
今天讲讲如何把中文的汉字转换为拼音首字母,如"中国"转换为“zg”,这里要用到一个开源jar包【pinyin4j

<span style="font-size:14px;">import net.sourceforge.pinyin4j.PinyinHelper;

/**
* 汉字转换为拼音首字母
* @author Strong
*
*/
public class WordToPinYin {

public static String toPinyin(String str){
String convert = "";
for (int j = 0, int len = str.length(); j < len; j++) {
char word = str.charAt(j);
String[] pinyinArray = PinyinHelper.toHanyuPinyinStringArray(word);
if (pinyinArray != null) {
convert += pinyinArray[0].charAt(0);
} else {
convert += word;
}
}
return convert;
}
}</span>
<span style="font-size:14px;">public static void main(String[] args){</span>
<span style="font-size:14px;">	String hzStr = "拼音";</span>


<span style="font-size:14px;">	System.out.printf(WordToPinYin.toPinyin(hzStr));</span>

<span style="font-size:14px;">}</span>

更多内容请参考java 将汉字转换为全拼及返回中文的首字母 




内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签:  java jar 开源