汉字助记码,你会了吗? 在编程中,我们经常会遇到汉字助记码的问题,笔者曾经为此多次发愁,现总结前辈的好东西,记录于此,希望能帮助到您,方法有多种,在此比较几种方案,简单剖析一下。
首先说明,什么是汉字助记码?所谓的汉字助记码就是一个汉字的拼音的首字母,如:张的汉字助记码为Z,湖北中医药大学的助记码为HBZYYDX。下面通过程序用三种方法实现:
方法一:表获取方法;
表内容大致说明:
实现核心代码——SQL标量值函数:
1------------------------------------------------2--作者:zhangbc3--时间:2014-05-054--功能:获取汉字拼音首字母5------------------------------------------------6ALTERfunction[dbo].[fun_getMnemonic](@strnvarchar(4000))7returnsnvarchar(4000)8as9begin10declare@zjmvarchar(100),@tmp_charvarchar(2),@tmp_zjmvarchar(2),@iint,@lengthint11set@zjm=''12set@length=len(@str)13set@i=114while@i<=@length15begin16set@tmp_char=substring(@str,@i,1)17select@tmp_zjm=zjmfromhz_zjmwherehanzi=@tmp_char18if@@rowcount=119set@zjm=@zjm+Rtrim(@tmp_zjm)20set@i=@i+121end22return(@zjm)23end
View Code
方法二:存储过程获取;
实现核心代码——SQL存储过程:
1--=============================================2--Author: zhangbc3--Create date: 2014-05-034--Description: 获取汉字拼音首字母5--=============================================6ALTERPROCEDURE[dbo].[getMnemonic]7@strvarchar(4000)8AS9--return char(4000)10BEGIN11declare@Wordnchar(1),@PYnvarchar(4000)12set@PY=''13whilelen(@str)>014begin15set@word=left(@str,1)16--如果非汉字字符,返回原字符17set@PY=@PY+(casewhenunicode(@word)between19968and19968+2090118then(selecttop1PYfrom(19select'A'asPY,N'驁'asword20unionallselect'B',N'簿'21unionallselect'C',N'錯'22unionallselect'D',N'鵽'23unionallselect'E',N'樲'24unionallselect'F',N'鰒'25unionallselect'G',N'腂'26unionallselect'H',N'夻'27unionallselect'J',N'攈'28unionallselect'K',N'穒'29unionallselect'L',N'鱳'30unionallselect'M',N'旀'31unionallselect'N',N'桛'32unionallselect'O',N'漚'33unionallselect'P',N'曝'34unionallselect'Q',N'囕'35unionallselect'R',N'鶸'36unionallselect'S',N'蜶'37unionallselect'T',N'籜'38unionallselect'W',N'鶩'39unionallselect'X',N'鑂'40unionallselect'Y',N'韻'41unionallselect'Z',N'咗'42) T43whereword>=@wordcollate Chinese_PRC_CS_AS_KS_WS44orderbyPYASC)else@wordend)45set@str=right(@str,len(@str)-1)46end47select@PY48END
View Code
方法三:标量值获取(算法和方法二一样,实现方式不同):
代码如下:
1------------------------------------------------2--作者:zhangbc3--时间:2014-03-194--功能:获取汉字拼音首字母5------------------------------------------------6ALTERfunction[dbo].[fun_getZjm](@strnvarchar(4000))7returnsnvarchar(4000)8as9begin10declare@wordnchar(1),@PYnvarchar(4000)11set@PY=''12whilelen(@str)>013begin14set@word=left(@str,1)15--如果非汉字字符,返回原字符16set@PY=@PY+(casewhenunicode(@word)between19968and19968+2090117then(selecttop1PYfrom(18select'A'asPY,N'驁'asword19unionallselect'B',N'簿'20unionallselect'C',N'錯'21unionallselect'D',N'鵽'22unionallselect'E',N'樲'23unionallselect'F',N'鰒'24unionallselect'G',N'腂'25unionallselect'H',N'夻'26unionallselect'J',N'攈'27unionallselect'K',N'穒'28unionallselect'L',N'鱳'29unionallselect'M',N'旀'30unionallselect'N',N'桛'31unionallselect'O',N'漚'32unionallselect'P',N'曝'33unionallselect'Q',N'囕'34unionallselect'R',N'鶸'35unionallselect'S',N'蜶'36unionallselect'T',N'籜'37unionallselect'W',N'鶩'38unionallselect'X',N'鑂'39unionallselect'Y',N'韻'40unionallselect'Z',N'咗'41) T42whereword>=@wordcollate Chinese_PRC_CS_AS_KS_WS43orderbyPYASC)else@wordend)44set@str=right(@str,len(@str)-1)45end46return@PY47end
View Code
方法四:字符编码法;
核心代码如下:
1publicclassgetMnemonic2{3publicgetMnemonic()4{56}7///<summary>8///字符编码的获取9///</summary>10///<param name="cnChar">字符</param>11///<returns>字符编码</returns>12privatestaticstringgetSpell(stringcnChar)13{14byte[] arrCN =Encoding.Default.GetBytes(cnChar);15if(arrCN.Length >1)16{17intarea = (short)arrCN[0];18intpos = (short)arrCN[1];19intcode = (area <<8) +pos;20int[] areacode = {45217,45253,45761,46318,46826,47010,47297,47614,2148119,48119,49062,49324,49896,50371,50614,50622,2250906,51387,51446,52218,52698,52698,52698,52980,53689,54481};23for(inti =0; i <26; i++)24{25intmax =55290;26if(i !=25)27max = areacode[i +1];28if(areacode[i] <= code && code <max)29returnEncoding.Default.GetString(newbyte[] { (byte)(65+i)});30}31return"*";32}33elsereturncnChar;34}35///<summary>36///字符编码获取助记码37///</summary>38///<param name="str">汉字</param>39///<returns>助记码</returns>40publicstringgetChsSpell(stringstr)41{42stringmyStr ="";43for(inti =0; i < str.Length; i++)44{45myStr += getSpell(str.Substring(i,1));46}47returnmyStr;48}49}
View Code
测试结果如下:
图1-1
图1-2
图1-3
图1-4
小结:
通过测试结果来看,字符编码(方法四)还是有点不完美,其实通过表获取的方法(方法一)也存在不足,汉字存储太少。
这次测试的一个意外收获就是char()与varchar()的区别,请看:
还望知情者给予合理的解释,不甚感激!如果能给你带来帮助,请赞一个,你的用心阅读是我写博的不竭动力!
赠送源码一份,供您研究:http://files.cnblogs.com/zhangbc/MnemonicOfWords.rar
PS:如有不足之处,欢迎指点与切磋,您的光临是我的荣幸,联系方式QQ:649414754