C # Get the first letter of Chinese Characters in pinyin

Source: Internet
Author: User

Today, I saw a "C # Calculate Chinese pinyin code" on the Blog of yyf9989. I thought it was complicated to read the code. It mainly retrieves the position of the input Chinese characters in the array and returns the first letter. So I remembered that I could use the Chinese character encoding in the computer to get the first Pinyin of the Chinese character. I found some information and solved it through the following methods. Put it here for your reference.
Static public string GetChineseSpell (string strText)
{
Int len = strText. Length;
String myStr = "";
For (int I = 0; I <len; I ++)
{
MyStr + = getSpell (strText. Substring (I, 1 ));
}
Return myStr;
}
Static public string getSpell (string cnChar)
{
Byte [] arrCN = Encoding. Default. GetBytes (cnChar );
If (arrCN. Length> 1)
{
Int area = (short) arrCN [0];
Int pos = (short) arrCN [1];
Int code = (area <8) + pos;
Int [] areacode = {clerk, 45761,46318, clerk, 47297,47614, clerk, clerk, 49896,50371, clerk, 50906,51387, clerk, clerk, 52698,52980, 5digit, 54481 };
For (int I = 0; I <26; I ++)
{
Int max = 55290;
If (I! = 25) max = areacode [I + 1];
If (areacode [I] <= code & code <max)
{
Return Encoding. Default. GetString (new byte [] {(byte) (65 + I )});
}
}
Return "*";
}
Else return cnChar;
}

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.