C # obtain the pinyin initials of a single character

Source: Internet
Author: User

Public class converttopinying
{
/// <Summary>
/// Abbreviated Chinese character to PinYin
/// </Summary>
/// <Param name = "str"> string of Chinese characters to be converted </param>
/// <Returns> pinyin abbreviation </returns>
Public static string getpystring (string Str)
{
String tempstr = "";
If (! Ischina (STR ))
{
Return STR;
}
Foreach (char C in Str)
{
If (INT) C >=33 & (INT) C <= 126)
{// Reserved letters and symbols
Tempstr + = C. tostring ();
}
Else
{// Accumulate the pinyin initials
Tempstr + = getpychar (C. tostring ());
}
}
Return tempstr;
}

Public static bool ischina (string cstring)
{
Bool boolvalue = false;
For (INT I = 0; I <cstring. length; I ++)
{
If (convert. toint32 (convert. tochar (cstring. substring (I, 1) <convert. toint32 (convert. tochar (128 )))
{
Boolvalue = false;
}
Else
{
Return boolvalue = true;
}
}
Return boolvalue;
}


/// <Summary>
/// Obtain the pinyin initials of a single character
/// </Summary>
/// <Param name = "C"> single Chinese character to be converted </param>
/// <Returns> pinyin initials </returns>
Private Static string getpychar (string C)
{
Byte [] array = new byte [2];
Array = system. Text. encoding. Default. getbytes (C );
Int I = (short) (array [0]-'\ 0') * 256 + (short) (array [1]-' \ 0 '));

If (I <0xb0a1) Return "*";
If (I <0xb0c5) Return "";
If (I <0xb2c1) Return "B ";
If (I <0xb4ee) Return "C ";
If (I <0xb6ea) Return "D ";
If (I <0xb7a2) Return "E ";
If (I <0xb8c1) Return "F ";
If (I <0xb9fe) Return "G ";
If (I <0xbbf7) Return "H ";
If (I <0xbfa6) Return "G ";
If (I <0xc0ac) Return "K ";
If (I <0xc2e8) Return "L ";
If (I <0xc4c3) Return "M ";
If (I <0xc5b6) Return "N ";
If (I <0xc5be) Return "O ";
If (I <0xc6da) Return "p ";
If (I <0xc8bb) Return "Q ";
If (I <0xc8f6) Return "R ";
If (I <0 xcbfa) Return "S ";
If (I <0 xcdda) Return "T ";
If (I <0xcef4) Return "W ";
If (I <0xd1b9) Return "X ";
If (I <0xd4d1) Return "Y ";
If (I <0xd7fa) Return "Z ";

Return "*";
}

}

 

C # obtain the pinyin initials of a single character

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.