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;
}