ASP.NET获取汉字拼音的首字母
2013-12-27 16:21
471 查看
#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) as begin declare @word nchar(1),@PY nvarchar(4000) set @PY='' while len(@str)>0 begin set @word=left(@str,1) set @PY=@PY+(case when unicode(@word) between 19968 and 19968+20901 then (select top 1 PY from ( select 'A' as PY,N'驁' as word union 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) end return @PY end --调用标致函数进行转换 select dbo.fun_getPY('中国龙')
相关文章推荐
- asp.net汉字转拼音和获取汉字首字母的代码
- asp.net汉字转拼音和获取汉字首字母的代码
- ASP.NET获取汉字拼音的首字母
- asp.net汉字转拼音和获取汉字首字母的代码
- ASP.NET获取汉字拼音的首字母
- ASP.NET获取汉字拼音的首字母
- ASP.NET 根据汉字获取汉字拼音的首字母(含多音字)
- ASP.NET 根据汉字获取汉字拼音的首字母(含多音字)
- asp.net汉字转拼音和获取汉字首字母的代码
- ASP中获取汉字拼音的第一个字母
- asp.net 汉字转换拼音及首字母实现代码
- c# asp.net 2.0 获取汉字第一个拼音
- 取得汉字拼音首字母 (asp.net C#)
- ASP.NET获取汉字首字母
- asp.net获取中文汉字的首字母
- asp.net 汉字转换拼音及首字母实现代码
- asp.net 自动将汉字转换成拼音第一个字母
- asp.net 汉字转换拼音及首字母实现代码
- asp.net 获取汉字首字母