对于一些列表数据,咱们须要进行按拼音字母排序(特别是省份城市),须要对于中文进行排序。java
代码以下工具
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; } }
下载地址:http://sourceforge.net/projects/pinyin4j/
pinyin4j项目的首页在 http://npinyin4j.codeplex.com/.net
其功能和使用可参考:参考博客1code
文章为原创,转载请注明出处。orm