C#根据汉字生成拼音首字母全称

时间:2022-01-27 20:45:39
 static void Main(string[] args)
{
string s = GetChineseSpell("周杰伦");
Console.WriteLine(s.ToLower());
Console.ReadKey();
}
static public string GetChineseSpell(string strText)
{
int len = strText.Length;
string myStr = "";
for (int i = ; i < len; i++)
{
    myStr += getSpell(strText.Substring(i, ));
}
return myStr;
} static public string getSpell(string cnChar)
{
byte[] arrCN = Encoding.Default.GetBytes(cnChar);
if (arrCN.Length > )
{
    int area = (short)arrCN[];
    int pos = (short)arrCN[];
    int code = (area << ) + pos;
    int[] areacode = { , , , , , , , , , , , , , , , , , , , , , , , , ,
  };
for (int i = ; i < ; i++)
{
int max = ;
if (i != ) max = areacode[i + ];
if (areacode[i] <= code && code < max)
{
    return Encoding.Default.GetString(new byte[] { (byte)( + i) });
}
}
return "*";
}
else return cnChar;
}

控制台运行结果为:

C#根据汉字生成拼音首字母全称