"JAVA" to get the first letter of Chinese pinyin

Source: Internet
Author: User

Recently, when you make your address book, you need to convert your last name to Phonetic Alphabet.

1-9 A-Z conversion to #; Let's take a picture.

There are a lot of code on the Internet, two kinds

1:pinyin4java bag is too big

2: Most do not support uncommon words, such as "Luan, Xin" do not recognize

This solution solves this problem, it is very simple a helper class, note, just pinyin initials OH ! and the encoding format is gbk!

The code is very simple, is in the normal gbk retrieval of uncommon words, and then add a dictionary, if the GBK can not be retrieved, then in the dictionary to find.

 Packagezhexian.app.smartcall.Utils;Importjava.io.UnsupportedEncodingException;ImportJava.util.HashMap;ImportJava.util.Map; Public classPinyinhelper {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 StaticMap<character, string> exceptwords =NewHashMap (); Static{exceptwords.put (' A ', ' um ao '); Exceptwords.put (' B ', "侼 and Kagenori."); Exceptwords.put (' C ', "Chigang E. Changer 丳 刅"); Exceptwords.put (' D ', "Du Dangshan pear di dan); Exceptwords.put (' F ', "Weifang 冹 兝"); Exceptwords.put (' G ', "gu ligustilide guan hack 丱 books dried foods 仠 nabrown 对应部件 匄"); Exceptwords.put (' H ', "Hua Hun Huang Huang Cabal attaining 匢"); Exceptwords.put (' J ', "Jing Jiao Ji Jin Jin ju bld 丩 丮 Square Bowl sanctioned still 冏 匊 匛 on"); Exceptwords.put (' K ', ' 丂 匟 '); Exceptwords.put (' L ', "Lao Lai Luan Li Luo Liu Lei sweet wine lu Langfeng lai tael Jiyu There"); Exceptwords.put (' M ', "Guanyintang mi miluo hideaway 冐 冺 兞 No"); Exceptwords.put (' O ', ' ou '); Exceptwords.put (' P ', ' Pei Shing 伂 clonazepam '); Exceptwords.put (' Q ', "Joe Ziqi qu Qi qu qiong 丠 footstep 亝 Î 兛 匤"); Exceptwords.put (' R ', "Banyan 刅"); Exceptwords.put (' S ', "Si sui shu Shengzhou xi Xin song ku Shang breakfast 侺 兙"); Exceptwords.put (' T ', "Tong Teng 侹 侻"); Exceptwords.put (' W ', "beauteous Weizhou Timor incompatible materials oxidizing executive swastika 卐"); Exceptwords.put (' X ', "Xin Gape Xun Ying Xixi had sex Karen killer"); Exceptwords.put (' Y ', "Yi Tire yi niuer yan Shangan aya 伇 偐 yen beast"); Exceptwords.put (' Z ', "Azusa Zhuo trillion zhen astir armatures instruction"); }     Public Staticstring Getpinyin (String str) {StringBuffer buffer=NewStringBuffer (Str.length ());  for(inti = 0; I < str.length (); i++) {buffer.append (Getpinyin (Str.charat (i))); }        returnbuffer.tostring (); }     Public Static CharGetpinyin (Charcharacter) {        if(character >= ' A ' && character <= ' Z ')            returncharacter; 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‘#‘; }            intArea = ( Short) bytes[0]; intpos = ( Short) bytes[1]; intCode = (Area << 8 & 0xff00) + (POS & 0xFF);  for(inti = 0; I < 26; i++) {                intmax = 55290; if(I! = 25) {Max= Area_code[i + 1]; }                if(Area_code[i] <= code && Code <max) {                    returnInitialtable[i]; }            }             for(Map.entry<character, string>LetterSet:exceptWords.entrySet ()) {                if(Letterset.getvalue (). INDEXOF (character)! =-1) {                    returnLetterset.getkey (); }            }            return‘#‘; }    }}

"JAVA" to get the first letter of Chinese pinyin

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.