Practical development tips for Windows Phone (34): Get the first letter of Chinese pinyin

Source: Internet
Author: User

In previous articles, I used to explain how to obtain all Chinese Characters in Windows Phone. At that time, I put all Chinese characters and Pinyin into one set and then matched the search results, this will cause a problem, that is, the Chinese characters in the set cannot be found in pinyin, and some exceptions may occur.

Today we will introduce a new method, using the open-source project GB2312 for Silverlight on codeplex.

1. Create a project named mangoGB2312

2. Copy the two files in the GB2312 for Silverlight project to the project directory GB2312 and change the namespace of GB2312Encoding. cs to mangoGB2312.GB2312, as shown in

3. Change the gb2312.bin generation method to an embedded resource, as shown in figure

4. Compile a function to extract the first letter of a Chinese character. I have encapsulated it into a help class. The Code is as follows:

Public class FirstLetterHelper {/// <summary> /// obtain the first letter of a Chinese character in pinyin, if it is an English letter, the system returns an uppercase letter // </summary> /// <param name = "CnChar"> single Chinese Character </param> /// <returns> single uppercase letter </returns> public static string GetCharSpellCode (string CnChar) {long iCnChar; GB2312Encoding encoder = new GB2312Encoding (); byte [] ZW = encoder. getBytes (CnChar); // if it is a letter, if (ZW. length = 1) {return CnChar. toUpper ();} else {// get the array of byte from the single char int i1 = (short) (ZW [0]); int i2 = (short) (ZW [1]); iCnChar = i1 * 256 + i2;} // iCnChar match the constant if (iCnChar> = 45217) & (iCnChar <= 45252 )) {return "A";} else if (iCnChar >=45253) & (iCnChar <= 45760) {return "B ";} else if (iCnChar> = 45761) & (iCnChar <= 46317) {return "C";} else if (iCnChar> = 46318) & (iCnChar <= 46825) {return "D";} else if (iCnChar> = 46826) & (iCnChar <= 47009 )) {return "E";} else if (iCnChar >=47010) & (iCnChar <= 47296) {return "F ";} else if (iCnChar> = 47297) & (iCnChar <= 47613) {return "G";} else if (iCnChar> = 47614) & (iCnChar <= 48118) {return "H";} else if (iCnChar> = 48119) & (iCnChar <= 49061 )) {return "J";} else if (iCnChar >=49062) & (iCnChar <= 49323) {return "K ";} else if (iCnChar> = 49324) & (iCnChar <= 49895) {return "L";} else if (iCnChar> = 49896) & (iCnChar <= 50370) {return "M";} else if (iCnChar> = 50371) & (iCnChar <= 50613 )) {return "N";} else if (iCnChar >=50614) & (iCnChar <= 50621) {return "O ";} else if (iCnChar> = 50622) & (iCnChar <= 50905) {return "P";} else if (iCnChar> = 50906) & (iCnChar <= 51386) {return "Q";} else if (iCnChar> = 51387) & (iCnChar <= 51445 )) {return "R";} else if (iCnChar >=51446) & (iCnChar <= 52217) {return "S ";} else if (iCnChar> = 52218) & (iCnChar <= 52697) {return "T";} else if (iCnChar> = 52698) & (iCnChar <= 52979) {return "W";} else if (iCnChar> = 52980) & (iCnChar <= 53640 )) {return "X";} else if (iCnChar >=53689) & (iCnChar <= 54480) {return "Y ";} else if (iCnChar >=54481) & (iCnChar <= 55289) {return "Z" ;}else return ("#");}}

5. Test the program. Select the Chinese Input Method on the keyboard of the simulator, as shown in figure

6. Enter some text and click GET to GET the first letter of the Chinese character.

You can find the source code of this article here

Related Article

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.