赞
踩
public static List<String> cityNameChar = new ArrayList<String>();//保存中文城市名; public static List<String> cityNameShort = new ArrayList<String>();//保存拼音首字母;
// 初始化城市字母简称集合 for (int i = 0; i < CityUtil.allCity.length; i++) { cityNameShort.add(getPingYinShort(CityUtil.allCity[i])); cityNameChar.add(CityUtil.allCity[i]); }
/** * 将字符串中的中文转化为拼音,并提取首字母 * * @param inputString * @return */ public static String getPingYinShort(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 shortString = ""; try { for (int i = 0; i < input.length; i++) { // \\u4E00是unicode编码,判断是不是中文 if (java.lang.Character.toString(input[i]).matches( "[\\u4E00-\\u9FA5]+")) { // 将汉语拼音的全拼存到temp数组 String[] temp = PinyinHelper.toHanyuPinyinStringArray( input[i], format); // 取拼音的第一个读音 shortString += temp[0].substring(0, 1); } } } catch (Exception e) { e.printStackTrace(); } return shortString; }
name = name.toLowerCase(); //字母搜索 if (name.matches("[a-zA-Z]+")) { for (int i = 0; i < CommonUtil.cityNameShort.size(); i++) { if (CommonUtil.cityNameShort.get(i).indexOf(name) != -1) { cityData.add(CommonUtil.cityNameChar.get(i)); } } return; } for (int i = 0; i < CityUtil.allCity.length; i++) {//这里查询了包含name的字符串,按需要定制; if (CityUtil.allCity[i].indexOf(name) != -1) { cityData.add(CityUtil.allCity[i]); } }
Copyright © 2003-2013 www.wpsshop.cn 版权所有,并保留所有权利。