C # obtain the initials of Chinese characters and Pinyin

Source: Internet
Author: User

/// <Summary>
/// Obtain the first pinyin letter of a single Chinese Character
/// </Summary>
/// <Param name = "cn"> </param>
/// <Returns> </returns>
Public static string getSpell (string cn)
{
# Region
Byte [] arrCN = Encoding. Default. GetBytes (cn );
If (arrCN. Length> 1)
{
Int area = (short) arrCN [0];
Int pos = (short) arrCN [1];
Int code = (area <8) + pos;
Int [] areacode = {45217,452 53, 45761,463 18, 46826,470 10, 47297,476 14, 48119,481 19, 49062,493 24, 49896,503 71, 50614,506 22, 50906,513 87, 51446,522 18, 52698,526 98, 52698,529 80, 53689,544 81 };
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 cn;
}
Else return cn;
# Endregion
}

/// <Summary>
/// Obtain the first letter of a Chinese character string
/// </Summary>
/// <Param name = "str"> </param>
/// <Returns> </returns>
Public static string GetStringSpell (string str)
{
Char [] chars = str. ToCharArray ();
String result = "";
If (chars. Length> 1)
{
For (int I = 0; I <chars. Length; I ++)
{
Result + = getSpell (chars [I]. ToString ());
}
}
Else if (chars. Length = 1)
{
Result = getSpell (str );
}

Return result. ToLower ();

}

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.