Recently I have a small item of an address book, you need to realize the function similar to Outlook, that is, find related contacts by the sound mother of the contact name, such as pressing Z, find out all the contacts of all the names, the other with pinyin Z is also It will be found, such as surname Zhao, Zheng ... Of course, Zidane (Zidane) will also be found.
I have the code implemented by Java, as follows: public static char getinitial (string str) {byte [] bs; try {bs = str.getbytes ("gb2312");} catch (java.io.unsupportedEncodingException E) {returnide ';} If (bs.length == 0) Return' * '; Switch (BS [0]) {case (Byte)' a ': Case (Byte)' A ': Return' a '; case (byte) 'b': case (byte) 'b': return 'b'; case (byte) 'c': Return 'C'; case (byte) 'd': case (byte) 'D': return 'D'; case (byte) 'e': return 'e'; case (byte) 'f': case (byte) 'f': return 'f' Case (byte) 'g': return 'g'; case (byte) 'h': Case (Byte) 'h': return 'h'; case (byte) 'i' : Case (Byte) 'i': Return 'I'; Case (Byte) 'J': Return 'J'; Case (Byte) 'K': Case (Byte) 'K' : Return 'K'; Case (Byte) 'L': Return 'L'; Case (Byte) 'M': Case (Byte) 'M': Return 'M'; Case BYTE) 'N': Case (Byte) 'N': Return 'N'; Case (Byte) 'o': Case (Byte) 'o': Case (Byte) 'P': Case BYTE) 'P': Return 'P'; Case (Byte) 'Q': Case (Byte ) 'Q': return 'q'; case (byte) 'r': return 'r'; case (byte) 's': case (byte)' s': return 's '; Case (byte)' t ': Case (byte)' t ': Return' T '; Case (Byte)' u ': Case (Byte)' u '
: Return 'u'; case (byte) 'V': ru ': Return' v '; case (byte)' w ': case (byte)' W ': Return' W '; CASE Byte) 'x': Case (byte) 'x': Case (Byte) 'Y': Case (Byte) 'Y': Return 'Y'; Case (Byte) 'Z': CASE BYTE) 'Z': Return 'Z'; default: if (BS.LENGTH> = 2) // Length> = 2 It is possible to be Chinese {int b1 = BS [0] & 0xff; Int b2 = BS [1] & 0xff; int value = (B1 << 8) | b2; system.out.println (value); if (value> = 0xB0A1 && value <= 0xB0C4) Return 'a'; if (value> = 0xB0C5 && value <= 0xB2C0) RETURN 'B'; if (Value> = 0xB2C1 && value <= 0xB4ed) Return 'c'; if (value> = 0xB4ee && value <= 0xB6e9) Return 'd'; if (Value> = 0xB6EA && Value < = 0xB7A1) RETURN 'E'; if (value> = 0xB7A2 && value <= 0xB8c0) Return 'f'; if (value> = 0xB8c1 && value <= 0xB9FD) Return 'g'; if (value> = 0xB9fe && value <= 0xbbf6) Return 'h'; if (value> = 0xBBF7 && value <= 0xBFA5) Return 'J'; if (value> = 0xBFA6 && value <= 0xc0ab) Return 'k'; if (value> = 0xc0ac &&&&& Value <= 0xc2e7) Return 'L'; if (Value> = 0xC2e8 && value <= 0xc4c2) Return ' m '; if (value> = 0xc4c3 && value <= 0xc5b5) Return' n '; if (value> = 0xc5b6 && value <= 0xc5bd) return' o '; if (value> = 0xc5be && value <=
0xC6D9) Return 'p'; if (value> = 0xc6da && value <= 0xc8ba) Return 'q'; if (value> = 0xc8bb && value <= 0xc8f5) Return 'R'; if (value> = 0xc8f6 && value < = 0xcbf9) Return 's'; if (value> = 0xcbfa && value <= 0xcdd9) Return 't'; if (value> = 0xcdda && value <= 0xcef3) Return 'w'; if (value> = 0xcef4 && value <= 0xD188) RETURN 'X'; if (Value> = 0xD1B9 && value <= 0xD4D0) Return 'y'; if (value> = 0xD4d1 && value <= 0xd7f9) Return 'Z';}} return '*'; } The code is actually very simple. If you want to write this, you need to know how GB2312 encoding, I think the information on the official website of HP is not bad, it is clear. The URL is here. There is such a paragraph: National Standard GB2312 is based on the "Basic Set of Han Character Coding Character Set" issued by 1980. It is Chinese national standard for Chinese information, which is a Chinese code for enforcement. The national code has a total of 6763 Simplified Chinese characters, 682 symbols, including 3,755 first-level words, sorted in pinyin, 3008 secondary words, and sort by deflection. The formulation and application of this standard has played a big role in promoting Chinese informationization processes. This standard uses a double byte to represent a Chinese character: High byte A1-F7 (wherein the character area A1-F9, the Chinese zone B0-F7) low byte A1-Fe. The above text is already very clear, the most commonly used 3755 Chinese characters are sorted in pinyin, so it is possible to use Pinyin to find Chinese characters in more convenient implementation. Also explained, there are some rare surnames can't find the above code, such as Taiwan has a host called Zhongkang. However, in my small project, I feel that I feel compiled, I want to use JavaScript to implement the functions implemented with Java. I will help Google again, I found http://www.blueidea.com/user/qswh/qswhgb2312.js. I can live online. Examples can take a look at http://www.blueidea.com/user/qswh/gb2312.html. This script is used to resolve the pinyin of Chinese characters.