ASP.NET获取汉字拼音的首字母
代码
1 #region GetChineseSpell获取汉字拼音的第一个字母
2 //获取汉字拼音的第一个字母
3 static public string GetChineseSpell(string strText)
4 {
5 int len = strText.Length;
6 string myStr = "";
7 for (int i = 0; i < len; i++)
8 {
9 myStr += getSpell(strText.Substring(i, 1));
10 }
11 return myStr;
12 }
13
14 static public string[] GetChineseSpell(string[] strText)
15 {
16 int len = strText.Length;
17 string[] myStr = null;
18 for (int i = 0; i < len; i++)
19 {
20 myStr[i] = getSpell(strText[i]);
21 }
22 return myStr;
23 }
24
25 static public string getSpell(string cnChar)
26 {
27 byte[] arrCN = Encoding.Default.GetBytes(cnChar);
28 if (arrCN.Length > 1)
29 {
30 int area = (short)arrCN[0];
31 int pos = (short)arrCN[1];
32 int code = (area << 8) + pos;
33 int[] areacode = { 45217, 45253, 45761, 46318, 46826, 47010, 47297, 47614, 48119, 48119, 49062, 49324, 49896, 50371, 50614, 50622, 50906, 51387, 51446, 52218, 52698, 52698, 52698, 52980, 53689, 54481 };
34 for (int i = 0; i < 26; i++)
35 {
36 int max = 55290;
37 if (i != 25) max = areacode[i + 1];
38 if (areacode[i] <= code && code < max)
39 {
40 return Encoding.Default.GetString(new byte[] { (byte)(65 + i) });
41 }
42 }
43 return "*";
44 }
45 else return cnChar;
46 }
47 #endregion

浙公网安备 33010602011771号