Sometimes, there may be a need for something like this:
For this effect, we can have a solution like this:
Packagebys.utils;Importjava.io.UnsupportedEncodingException;/*** Created by Toutou on 2014/2/21*/ Public classChinesecharacterhelper {Static Final intGb_sp_diff = 160; //The starting location code for the different pronunciations of the Chinese national standard first Class Static Final int[] Secposvaluelist = {1601, 1637, 1833, 2078, 2274, 2302, 2433, 2594, 2787, 3106, 3212, 3472, 3635, 3722, 3730, 3858, 4027, 4086, 4390, 4558, 4684, 4925, 5249, 5600 }; //storage of national standard first-class Chinese characters different pronunciation of the starting location code corresponding pronunciation Static Final Char[] Firstletter = {' A ', ' B ', ' C ', ' d ', ' e ', ' f ', ' g ', ' H ', ' J ', ' K ', ' l ', ' m ', ' n ', ' o ', ' P ', ' Q ', ' R ', ' s ', ' t ', ' W ', ' x ', ' y ', ' z ' }; /*** Extract the first letter of the Chinese character string *@paramcharacters Kanji String *@return */ Public Staticstring Getspells (string characters) {StringBuffer buffer=NewStringBuffer (); for(inti = 0; I < characters.length (); i++) { CharCH =Characters.charat (i); if(ch >> 7) = = 0) { //determines whether the Chinese character, if left 7 for 0 is not a Chinese character, otherwise it is a Chinese characterbuffer.append (CH); } Else { CharSpell =getfirstletter (CH); Buffer.append (string.valueof (spell)); } } returnbuffer.tostring (); } /*** Get the first letter of a Chinese character *@paramCH Kanji *@return */ Public StaticCharacter Getfirstletter (Charch) { byte[] UniCode =NULL; Try{UniCode= string.valueof (ch). GetBytes ("GBK"); } Catch(unsupportedencodingexception e) {e.printstacktrace (); return NULL; } if(Unicode[0] < && unicode[0] > 0) {//Non-kanji return NULL; } Else { returnconvert (UniCode); } } /*** Get the pinyin initials of a Chinese character. GB code two bytes minus 160, converted to 10 binary code combination can be obtained location code * For example, the Chinese character "You" GB code is 0XC4/0XE3, minus 0xa0 (160) is 0x24/0x43 * 0x24 into 10 is 36,0x43 is 67, then its location Code is 3667, in the comparison table, the pronunciation of ' n '*/ Private Static CharConvertbyte[] bytes) { Charresult = ' # '; intSecposvalue = 0; inti; for(i = 0; i < bytes.length; i++) {Bytes[i]-=Gb_sp_diff; } secposvalue= bytes[0] * + bytes[1]; for(i = 0; i < i++;) { if(Secposvalue >= secposvaluelist[i] && Secposvalue < Secposvaluelist[i + 1]) {result=Firstletter[i]; Break; } } returnresult; }}
Invoke ExampleString carName = ChineseCharacterHelper.getSpells(String.valueOf(user.getName().charAt(0))).toUpperCase();
Java to take the first letter of Chinese characters