![ASP.NET获取汉字首字母 ASP.NET获取汉字首字母](https://image.shishitao.com:8440/aHR0cHM6Ly9ia3FzaW1nLmlrYWZhbi5jb20vdXBsb2FkL2NoYXRncHQtcy5wbmc%2FIQ%3D%3D.png?!?w=700&webp=1)
/// <summary>
/// 获取汉字首字母(可包含多个汉字)
/// </summary>
/// <param name="strText"></param>
/// <returns></returns>
public string GetChineseSpell(string strText)
{
int len = strText.Length;
string myStr = "";
for (int i = ; i < len; i++)
{
myStr += getSpell(strText.Substring(i, ));
}
return myStr;
} 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;
} ===============================================================================================
/// <summary>
/// 获取第一个汉字的首字母,只能输入汉字
/// </summary>
/// <param name="c"></param>
/// <returns></returns>
public string GetPYChar(string c)
{
byte[] array = new byte[];
array = System.Text.Encoding.Default.GetBytes(c);
int i = (short)(array[] - '\0') * + ((short)(array[] - '\0'));
if (i < 0xB0A1) return "*";
if (i < 0xB0C5) return "A";
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 "J";
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 < 0xCBFA) return "S";
if (i < 0xCDDA) return "T";
if (i < 0xCEF4) return "W";
if (i < 0xD1B9) return "X";
if (i < 0xD4D1) return "Y";
if (i < 0xD7FA) return "Z";
return "*";
}