In a project, you need to make queries based on the first letter of certain query conditions (such as name). For example, if you want to query a person named "James", you can enter 'zs '. A tool class is written as follows:
/** GB 2312-80 divides the recorded Chinese characters into two levels. The first level of Chinese characters is commonly used, with 3755 Chinese characters counted,
* Place in 16 ~ Area 55, sorted by Chinese pinyin letters/PEN; second-level Chinese characters are commonly used Chinese characters,
* The number is 3008, which is placed between 56 and 56 ~ Area 87, arranged by the beginning/strokes, so this program can only find
* Specifies the initials of a level-1 Chinese character. At the same time, only the first letter (z, c, s) can be obtained for the compliant initials (zh, ch, sh)
*/
Java code
Static final int GB_SP_DIFF = 160;
// The initial location code for storing different pronunciations of the first-level Chinese Character of the National Standard
Static final int [] secPosValueList = {1601,163 7, 1833,207 8, 2274,230 2, 2433,259 4, 2787,310 6, 3212,347 2, 3635,372 2, 3730,385 8, 4027,408 6, 4390,455 8, 4684,492 5,
5249,560 0 };
// Stores the pronunciation of the starting location code corresponding to different pronunciations of the first-level Chinese Character of the National Standard
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 '};
// Obtain the pinyin code of a string
Public static String getFirstLetter (String oriStr ){
String str = oriStr. toLowerCase ();
StringBuffer buffer = new StringBuffer ();
Char ch;
Char [] temp;
For (int I = 0; I <str. length (); I ++) {// process each character in str in sequence
Ch = str. charAt (I );
Temp = new char [] {ch };
Byte [] uniCode = new String (temp). getBytes ();
If (uniCode [0] <128 & uniCode [0]> 0) {// non-Chinese Characters
Buffer. append (temp );
} Else {
Buffer. append (convert (uniCode ));
}
}
Return buffer. toString ();
}
// Obtain the pinyin code of a Chinese character
Public static Character getFirstLetter (char ch ){
Byte [] uniCode = null;
Try {
UniCode = String. valueOf (ch). getBytes ("GBK ");
} Catch (UnsupportedEncodingException e ){
E. printStackTrace ();
Return null;
}
If (uniCode [0] <128 & uniCode [0]> 0) {// non-Chinese Characters
Return null;
} Else {
Return convert (uniCode );
}
}
/**
* Obtain the first letter of a Chinese character. The two bytes of the GB code minus 160, respectively, and are converted into a 10-digit combination to get the location code.
* For example, if the GB code of the Chinese character "you" is 0xC4/0xE3, the difference between 0xA0 (160) and 0x24/0x43
* If 0x24 is converted to 10, it is 36, and 0x43 is 67, then its location code is 3667. In the comparison table, it is pronounced as 'n'
*/
Static char convert (byte [] bytes ){
Char result = '-';
Int secPosValue = 0;
Int I;
For (I = 0; I <bytes. length; I ++ ){
Bytes [I]-= GB_SP_DIFF;
}
SecPosValue = bytes [0] * 100 + bytes [1];
For (I = 0; I <23; I ++ ){
If (secPosValue> = secPosValueList [I] & secPosValue <secPosValueList [I + 1]) {
Result = firstLetter [I];
Break;
}
}
Return result;
}
Author "eric-gcm"