中文转拼音
HanYuToString 工具类
public class HanYuToString {
private final static HanyuPinyinOutputFormat format = new HanyuPinyinOutputFormat();
/**
* 汉字转字符串(使用format)
* @param chinese
* @return
* @throws BadHanyuPinyinOutputFormatCombination
*/
public static String toString(String chinese) throws BadHanyuPinyinOutputFormatCombination {
StringBuilder stringBuilder = new StringBuilder();
for (char ch : chinese.toCharArray()) {
String[] strings = PinyinHelper.toHanyuPinyinStringArray(ch,format);
System.out.println(Arrays.toString(strings));
stringBuilder.append(strings[0]);
}
return stringBuilder.toString();
}
/**
* 不带音调标记 eg 中国 zhongguo
* @param chinese
* @return
* @throws BadHanyuPinyinOutputFormatCombination
*/
public static String toStringWithOutTone(String chinese) throws BadHanyuPinyinOutputFormatCombination {
format.setToneType(HanyuPinyinToneType.WITHOUT_TONE);
return toString(chinese);
}
/**
* 带音调 eg 中国 zhong1guo2
* @param chinese
* @return
*/
public static String toStringWithTone(String chinese) throws BadHanyuPinyinOutputFormatCombination {
format.setToneType(HanyuPinyinToneType.WITH_TONE_NUMBER);
return toString(chinese);
}
}