pinyin4j 中文转化拼音 牛刀小试

今天小玩了一下pinyin4j,该组件很轻量,代码很少,测试用例在源码包中已经有了,我只是造了个小轮胎而已,在这里先保留,为以后用到做个储备

 

 

import net.sourceforge.pinyin4j.PinyinHelper;
import net.sourceforge.pinyin4j.format.HanyuPinyinOutputFormat;
import net.sourceforge.pinyin4j.format.HanyuPinyinToneType;
import net.sourceforge.pinyin4j.format.HanyuPinyinVCharType;
import net.sourceforge.pinyin4j.format.exception.BadHanyuPinyinOutputFormatCombination;


public class PinTest {

	public static void main(String[] args) {
		String aa = "中国传媒大学";
		for (char b:aa.toCharArray()){
			HanyuPinyinOutputFormat outputFormat = new HanyuPinyinOutputFormat();
			outputFormat.setVCharType(HanyuPinyinVCharType.WITH_V);
			outputFormat.setToneType(HanyuPinyinToneType.WITHOUT_TONE);
			try {
				String[] pa = PinyinHelper.toHanyuPinyinStringArray(b,outputFormat);
				System.out.print(pa[0]);
			} catch (BadHanyuPinyinOutputFormatCombination e) {
				e.printStackTrace();
			}
		}
	}
}

 

打印结果:

zhongguochuanmeidaxue

 

 

你可能感兴趣的:(pinyin4j)