asp.net(c#) 生成汉字字符串拼音首个字母的方法
1 using System;
2 using System.Collections.Generic;
3 using System.Text;
4
5
6 //获取汉字首字母
7 public static string GetChineseSpell(string strText)
8 {
9 string myStr = "";
10 if (!string.IsNullOrEmpty(strText))
11 {
12 int len = strText.Length;
13 for (int i = 0; i < len; i++)
14 {
15 myStr += getSpell(strText.Substring(i, 1));
16 }
17 }
18 return myStr;
19 }
20 private static string getSpell(string cnChar)
21 {
22 byte[] arrCN = Encoding.Default.GetBytes(cnChar);
23 if (arrCN.Length > 1)
24 {
25 int area = (short)arrCN[0];
26 int pos = (short)arrCN[1];
27 int code = (area << 8) + pos;
28 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 };
29 for (int i = 0; i < 26; i++)
30 {
31 int max = 55290;
32 if (i != 25) max = areacode[i + 1];
33 if (areacode[i] <= code && code < max)
34 {
35 return Encoding.Default.GetString(new byte[] { (byte)(65 + i) });
36 }
37 }
38 return "*";
39 }
40 else return cnChar;
41 }
2 using System.Collections.Generic;
3 using System.Text;
4
5
6 //获取汉字首字母
7 public static string GetChineseSpell(string strText)
8 {
9 string myStr = "";
10 if (!string.IsNullOrEmpty(strText))
11 {
12 int len = strText.Length;
13 for (int i = 0; i < len; i++)
14 {
15 myStr += getSpell(strText.Substring(i, 1));
16 }
17 }
18 return myStr;
19 }
20 private static string getSpell(string cnChar)
21 {
22 byte[] arrCN = Encoding.Default.GetBytes(cnChar);
23 if (arrCN.Length > 1)
24 {
25 int area = (short)arrCN[0];
26 int pos = (short)arrCN[1];
27 int code = (area << 8) + pos;
28 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 };
29 for (int i = 0; i < 26; i++)
30 {
31 int max = 55290;
32 if (i != 25) max = areacode[i + 1];
33 if (areacode[i] <= code && code < max)
34 {
35 return Encoding.Default.GetString(new byte[] { (byte)(65 + i) });
36 }
37 }
38 return "*";
39 }
40 else return cnChar;
41 }

浙公网安备 33010602011771号