首页 > Web开发 > 详细

.net 汉字转拼音首字母

时间:2016-03-24 10:17:01      阅读:231      评论:0      收藏:0      [点我收藏+]
  •  1 private void button1_Click(object sender, EventArgs e)
     2         {
     3             var value = this.textBox1.Text;
     4             var aa = GetChineseSpell(value);
     5             this.textBox2.Text = aa;
     6         }
     7 
     8         public string GetChineseSpell(string strText)
     9         {
    10             int len = strText.Length;
    11             string myStr = "";
    12             for (int i = 0; i < len; i++)
    13             {
    14                 myStr += getSpell(strText.Substring(i, 1));
    15             }
    16             return myStr;
    17         }
    18 
    19         public string getSpell(string cnChar)
    20         {
    21             byte[] arrCN = Encoding.Default.GetBytes(cnChar);
    22             if (arrCN.Length > 1)
    23             {
    24                 int area = (short)arrCN[0];
    25                 int pos = (short)arrCN[1];
    26                 int code = (area << 8) + pos;
    27                 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 };
    28                 for (int i = 0; i < 26; i++)
    29                 {
    30                     int max = 55290;
    31                     if (i != 25) max = areacode[i + 1];
    32                     if (areacode[i] <= code && code < max)
    33                     {
    34                         return Encoding.Default.GetString(new byte[] { (byte)(65 + i) });
    35                     }
    36                 }
    37                 return "*";
    38             }
    39             else return cnChar;
    40         }

     

.net 汉字转拼音首字母

原文:http://www.cnblogs.com/paihuai/p/5314239.html

(0)
(0)
   
举报
评论 一句话评论(0
关于我们 - 联系我们 - 留言反馈 - 联系我们:wmxa8@hotmail.com
© 2014 bubuko.com 版权所有
打开技术之扣,分享程序人生!