标签:
最近在做通讯录的时候,需要把姓转换为拼音字母
1-9 a-z A-Z 转换为#;借张微信的图大家感受下
网上的代码很多,不外乎两种
1:pinyin4java包太大
2:大部分不支持生僻字,比如“栾、鑫” 认不出
本方案解决了这个问题,就很简单一个helper类,注意,只是拼音首字母哦!且编码格式为GBK!
代码很简单,就是在正常GBK检索不到的生僻字上,再加入一个字典,如果GBK检索不到,则在字典里找。
package zhexian.app.smartcall.Utils; import java.io.UnsupportedEncodingException; import java.util.HashMap; import java.util.Map; public class PinYinHelper { private static final int[] AREA_CODE = new int[]{ 45217, 45253, 45761, 46318, 46826, 47010, 47297, 47614, 48119, 48119, 49062, 49324, 49896, 50371, 50614, 50622, 50906, 51387, 51446, 52218, 52698, 52698, 52698, 52980, 53689, 54481 }; private static char[] initialTable = {‘A‘, ‘B‘, ‘C‘, ‘D‘, ‘E‘, ‘F‘, ‘G‘, ‘H‘, ‘i‘, ‘J‘, ‘K‘, ‘L‘, ‘M‘, ‘N‘, ‘O‘, ‘P‘, ‘Q‘, ‘R‘, ‘S‘, ‘T‘, ‘U‘, ‘V‘, ‘W‘, ‘X‘, ‘Y‘, ‘Z‘,}; private static Map<Character, String> exceptWords = new HashMap(); static { exceptWords.put(‘A‘, "庵鳌"); exceptWords.put(‘B‘, "璧亳並侼別匂"); exceptWords.put(‘C‘, "茌丞丒丳刅"); exceptWords.put(‘D‘, "渎砀棣儋丟"); exceptWords.put(‘F‘, "邡冹兝"); exceptWords.put(‘G‘, "崮藁莞丐丱乢亁仠冮匃匄"); exceptWords.put(‘H‘, "骅珲潢湟丆冴匢"); exceptWords.put(‘J‘, "泾蛟暨缙旌莒鄄丌丩丮丯丼亅伋冏匊匛匞"); exceptWords.put(‘K‘, "丂匟"); exceptWords.put(‘L‘, "崂涞栾溧漯浏耒醴泸阆崃両刢劽啰"); exceptWords.put(‘M‘, "渑汨丏冐冺兞冇"); exceptWords.put(‘O‘, "瓯"); exceptWords.put(‘P‘, "邳濮郫丕伂冸"); exceptWords.put(‘Q‘, "喬綦衢岐朐邛丠丬亝冾兛匤"); exceptWords.put(‘R‘, "榕刄"); exceptWords.put(‘S‘, "泗睢沭嵊歙莘嵩鄯丄丗侺兙"); exceptWords.put(‘T‘, "潼滕郯亣侹侻"); exceptWords.put(‘W‘, "婺涠汶亾仼卍卐"); exceptWords.put(‘X‘, "鑫盱浔荥淅浠亵丅伈兇"); exceptWords.put(‘Y‘, "懿眙黟颍兖郓偃鄢晏丣亜伇偐円匜"); exceptWords.put(‘Z‘, "梓涿诏柘秭圳伀冑刣"); } public static String getPinYin(String str) { StringBuffer buffer = new StringBuffer(str.length()); for (int i = 0; i < str.length(); i++) { buffer.append(getPinYin(str.charAt(i))); } return buffer.toString(); } public static char getPinYin(char character) { if (character >= ‘A‘ && character <= ‘Z‘) return character; else if (character >= ‘a‘ && character <= ‘z‘) return (char) (character + 32); else if (character >= ‘0‘ && character <= ‘9‘) return ‘#‘; else { byte[] bytes; try { bytes = String.valueOf(character).getBytes("GBK"); } catch (UnsupportedEncodingException e) { e.printStackTrace(); return ‘#‘; } int area = (short) bytes[0]; int pos = (short) bytes[1]; int code = (area << 8 & 0xff00) + (pos & 0xff); for (int i = 0; i < 26; i++) { int max = 55290; if (i != 25) { max = AREA_CODE[i + 1]; } if (AREA_CODE[i] <= code && code < max) { return initialTable[i]; } } for (Map.Entry<Character, String> letterSet : exceptWords.entrySet()) { if (letterSet.getValue().indexOf(character) != -1) { return letterSet.getKey(); } } return ‘#‘; } } }
标签:
原文地址:http://www.cnblogs.com/kimmy/p/4563927.html