今天在网上看到有这个功能,就练习了一下,代码如下!
需要jar包 JPinyin.jar 链接如下:http://pan.baidu.com/s/1boTrcIj
package Test;
import com.github.stuxuhai.jpinyin.PinyinFormat;
import com.github.stuxuhai.jpinyin.PinyinHelper;
public class Test {
public static void main(String[] args) {
String str = "你好世界!";
String world1 = PinyinHelper.convertToPinyinString(str, ",", PinyinFormat.WITH_TONE_MARK); // nǐ,hǎo,shì,jiè
String world2 = PinyinHelper.convertToPinyinString(str, ",", PinyinFormat.WITH_TONE_NUMBER); // ni3,hao3,shi4,jie4
String world3 = PinyinHelper.convertToPinyinString(str, ",", PinyinFormat.WITHOUT_TONE); // ni,hao,shi,jie
PinyinHelper.getShortPinyin(str);
System.out.println(world1);
System.out.println(world2);
System.out.println(world3);
}
}