ASP.NET获取汉字拼音的首字母

来源:互联网 发布:js排序函数 编辑:程序博客网 时间:2024/04/26 21:29
#region GetChineseSpell获取汉字拼音的第一个字母        //获取汉字拼音的第一个字母        public static string GetChineseSpell(string strText)        {            int len = strText.Length;            string myStr = "";            for (int i = 0; i < len; i++)            {                myStr += getSpell(strText.Substring(i, 1));            }            return myStr;        }        public static string[] GetChineseSpell(string[] strText)        {            int len = strText.Length;            string[] myStr = null;            for (int i = 0; i < len; i++)            {                myStr[i] = getSpell(strText[i]);            }            return myStr;        }        public static string getSpell(string cnChar)        {            byte[] arrCN = Encoding.Default.GetBytes(cnChar);            if (arrCN.Length > 1)            {                int area = (short)arrCN[0];                int pos = (short)arrCN[1];                int code = (area << 8) + pos;                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 };                for (int i = 0; i < 26; i++)                {                    int 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 cnChar;            }        }        #endregion
 
 
 
 
 
 
另一种写法,数据库转换写法
 
create function fun_getPY(@str nvarchar(4000))returns nvarchar(4000)asbegindeclare @word nchar(1),@PY nvarchar(4000)set @PY=''while len(@str)>0beginset @word=left(@str,1)set @PY=@PY+(case when unicode(@word) between 19968 and 19968+20901then (select top 1 PY from (select 'A' as PY,N'驁' as wordunion all select 'B',N'簿'union all select 'C',N'錯'union all select 'D',N'鵽'union all select 'E',N'樲'union all select 'F',N'鰒'union all select 'G',N'腂'union all select 'H',N'夻'union all select 'J',N'攈'union all select 'K',N'穒'union all select 'L',N'鱳'union all select 'M',N'旀'union all select 'N',N'桛'union all select 'O',N'漚'union all select 'P',N'曝'union all select 'Q',N'囕'union all select 'R',N'鶸'union all select 'S',N'蜶'union all select 'T',N'籜'union all select 'W',N'鶩'union all select 'X',N'鑂'union all select 'Y',N'韻'union all select 'Z',N'咗') T where word>=@word collate Chinese_PRC_CS_AS_KS_WS order by PY ASC) else @word end)set @str=right(@str,len(@str)-1)endreturn @PYend--调用标致函数进行转换select dbo.fun_getPY('中国龙')


0 0
原创粉丝点击