汉字转拼音
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();
}
}
本文介绍了一个名为TinyPinyin的Java工具类,该工具可以将汉字转换为拼音。通过使用TinyPinyin库,可以轻松实现字符串中每个汉字到其对应拼音的转换,并保留非汉字字符不变。

772

被折叠的 条评论
为什么被折叠?



