ASP.NET获取汉字首字母

 1     /// <summary>
 2     /// 获取汉字首字母(可包含多个汉字)
 3     /// </summary>
 4     /// <param name="strText"></param>
 5     /// <returns></returns>
 6     public string GetChineseSpell(string strText)
 7     {
 8         int len = strText.Length;
 9         string myStr = "";
10         for (int i = 0; i < len; i++)
11         {
12             myStr += getSpell(strText.Substring(i, 1));
13         }
14         return myStr;
15     }
16 
17     public string getSpell(string cnChar)
18   {
19       byte[] arrCN = Encoding.Default.GetBytes(cnChar);
20       if (arrCN.Length > 1)
21       {
22           int area = (short)arrCN[0];
23           int pos = (short)arrCN[1];
24           int code = (area << 8) + pos;
25           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 };
26           for (int i = 0; i < 26; i++)
27           {
28               int max = 55290;
29               if (i != 25) max = areacode[i + 1];
30               if (areacode[i] <= code && code < max)
31               {
32                   return Encoding.Default.GetString(new byte[] { (byte)(65 + i) });
33               }
34           }
35           return "*";
36       }
37       else 
38         return cnChar;
39   }
40 
41 ===============================================================================================
42     /// <summary>
43     /// 获取第一个汉字的首字母,只能输入汉字
44     /// </summary>
45     /// <param name="c"></param>
46     /// <returns></returns>
47     public string GetPYChar(string c)
48         {
49             byte[] array = new byte[2];
50             array = System.Text.Encoding.Default.GetBytes(c);
51             int i = (short)(array[0] - '\0') * 256 + ((short)(array[1] - '\0'));
52             if (i < 0xB0A1) return "*";
53             if (i < 0xB0C5) return "A";
54             if (i < 0xB2C1) return "B";
55             if (i < 0xB4EE) return "C";
56             if (i < 0xB6EA) return "D";
57             if (i < 0xB7A2) return "E";
58             if (i < 0xB8C1) return "F";
59             if (i < 0xB9FE) return "G";
60             if (i < 0xBBF7) return "H";
61             if (i < 0xBFA6) return "J";
62             if (i < 0xC0AC) return "K";
63             if (i < 0xC2E8) return "L";
64             if (i < 0xC4C3) return "M";
65             if (i < 0xC5B6) return "N";
66             if (i < 0xC5BE) return "O";
67             if (i < 0xC6DA) return "P";
68             if (i < 0xC8BB) return "Q";
69             if (i < 0xC8F6) return "R";
70             if (i < 0xCBFA) return "S";
71             if (i < 0xCDDA) return "T";
72             if (i < 0xCEF4) return "W";
73             if (i < 0xD1B9) return "X";
74             if (i < 0xD4D1) return "Y";
75             if (i < 0xD7FA) return "Z";
76             return "*";
77         }

 

转载于:https://www.cnblogs.com/a-dou/articles/4817948.html

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值