获得中文的首字母

#region 获得中文的首字母
    protected String getSpell(String cnChar)
    {
        String first = cnChar.Substring(0, 1);
        byte[] arrCN = Encoding.Default.GetBytes(first);
        if (arrCN.Length > 1)
        {
            Int32 area = (short)arrCN[0];
            Int32 pos = (short)arrCN[1];
            Int32 code = (area << 8) + pos;
            Int32[] 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};
            for (Int32 i = 0; i < 26; i++)
            {
                Int32 max = 55290;
                if (i!= 25) max = areacode[i + 1];
                if (areacode[i] <= code && code < max)
                {
                    return Encoding.Default.GetString(new byte[] { (byte)(65 + i) });
                }
            }
            return "*";
        }
        else return first;
    }
    #endregion
posted @ 2009-04-29 16:51  非常菜鸟  阅读(153)  评论(0编辑  收藏  举报