脚本宝典收集整理的这篇文章主要介绍了Android 拼音处理工具类,脚本宝典觉得挺不错的,现在分享给大家,也给大家做个参考。
AndROId 拼音转字符串工具类封装
对于一些列表数据,我们需要进行按拼音字母排序(特别是省份城市),需要对于中文进行排序。
代码如下
public class PingYinUtil {
/**
* 将字符串中的中文转化为拼音,其他字符不变
*
* @param inputString
* @return
*/
public static String getPingYin(String inputString) {
HanyuPinyinOutputFormat format = new
HanyuPinyinOutputFormat();
format.setCaseType(HanyuPinyinCaseType.LOWERCASE);
format.setToneType(HanyuPinyinToneType.WITHOUT_TONE);
format.setVCharType(HanyuPinyinVCharType.WITH_V);
char[] input = inputString.trim().toCharArray();
String output = "";
try {
for (int i = 0; i < input.length; i++) {
if (java.lang.Character.toString(input[i]).
matches("[\u4E00-\u9FA5]+")) {
String[] temp = PinyinHelper.
toHanyuPinyinStringArray(input[i],
format);
output += temp[0];
} else
output += java.lang.Character.toString(
input[i]).toLowerCase();
}
} catch (BadHanyuPinyinOutputFormatCombination e) {
e.PRintStackTrace();
}
return output;
}
}
里面需要用到pinyin4j包
下载地址:http://sourceforge.net/projects/pinyin4j/
pinyin4j项目的首页在 http://npinyin4j.codeplex.com/
其功能和使用可参考:参考博客1
以上是脚本宝典为你收集整理的Android 拼音处理工具类全部内容,希望文章能够帮你解决Android 拼音处理工具类所遇到的问题。
本图文内容来源于网友网络收集整理提供,作为学习参考使用,版权属于原作者。
如您有任何意见或建议可联系处理。小编QQ:384754419,请注明来意。