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

获取汉字首字母

2013年11月22日 ⁄ 综合 ⁄ 共 887字 ⁄ 字号 评论关闭
static public string getSpell(string myChar)
    {
        
byte[] arrCN = System.Text.Encoding.Default.GetBytes(myChar);
        
if (arrCN.Length > 1)
        {
            
int area = (short)arrCN[0];
            
int pos = (short)arrCN[1];
            
int code = (area << 8+ pos;
            
int[] areacode = { 4521745253457614631846826470104729747614481194811949062493244989650371506145062250906513875144652218526985269852698529805368954481 };
            
for (int i = 0; i < 26; i++)
            {
                
int max = 55290;
                
if (i != 25) max = areacode[i + 1];
                
if (areacode[i] <= code && code < max)
                {
                    
return System.Text.Encoding.Default.GetString(new byte[] { (byte)(65 + i) });
                }
            }
            
return "*";
        }
        
else return myChar;
    }

 

抱歉!评论已关闭.