现在的位置: 首页 > 综合 > 正文

c#中文汉字首字母自动生成代码

2012年10月08日 ⁄ 综合 ⁄ 共 786字 ⁄ 字号 评论关闭
 1 //取汉字首字母
2 private string GetChineseSpell(string strText)
3 {
4 int len = strText.Length;
5 string myStr = "";
6 for (int i = 0; i < len; i++)
7 {
8 myStr += getSpell(strText.Substring(i, 1));
9 }
10 return myStr;
11 }
12 //取首字母方法
13 static public string getSpell(string myChar)
14 {
15 byte[] arrCN = System.Text.Encoding.Default.GetBytes(myChar);
16 if (arrCN.Length > 1)
17 {
18 int area = (short)arrCN[0];
19 int pos = (short)arrCN[1];
20 int code = (area << 8) + pos;
21 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 };
22 for (int i = 0; i < 26; i++)
23 {
24 int max = 55290;
25 if (i != 25) max = areacode[i + 1];
26 if (areacode[i] <= code && code < max)
27 {
28 return System.Text.Encoding.Default.GetString(new byte[] { (byte)(65 + i) });
29 }
30 }
31 return "*";
32 }
33 else return myChar;
34 }

 

抱歉!评论已关闭.