汉字转拼音
2018/2/5 13:58:54
使用库 TinyPinyin
https://github.com/promeG/TinyPinyin
工具类
import com.github.promeg.pinyinhelper.Pinyin;
/**
* Created with IDEA
* user:maohuawei
* <p>
* TinyPinyin 工具类
* <a href= https://github.com/promeG/TinyPinyin></a>
*/
public class PinyinUtil {
/**
* 汉字拼音
*
* @param hanzi
* @return
*/
public static String hanZi2Pinyin(String hanzi) {
if (hanzi == null) {
throw new NullPointerException("hanzi null");
}
StringBuilder stringBuilder = new StringBuilder();
char[] chars = hanzi.toCharArray();
for (char c : chars) {
if (Character.isWhitespace(c)) {
continue;
}
if (c > 127) {
String s = Pinyin.toPinyin(c);
stringBuilder.append(s);
} else {
stringBuilder.append(c);
}
}
return stringBuilder.toString();
}
}