在编程中,我们经常会遇到汉字助记码的问题,笔者曾经为此多次发愁,现总结前辈的好东西,记录于此,希望能帮助到您,方法有多种,在此比较几种方案,简单剖析一下。
首先说明,什么是汉字助记码?所谓的汉字助记码就是一个汉字的拼音的首字母,如:"张"的汉字助记码为Z,"湖南中医药大学“的助记码为HNZYYDX。下面通过程序用三种方法实现:
方法一:表获取方法;
表内容大致说明:
实现核心代码——SQL标量值函数:
<span style="font-size:18px;">------------------------------------------------
--作者:zhangbc
--时间:2014-05-25
--功能:获取汉字拼音首字母
------------------------------------------------
CREATE function [dbo].[fun_getMnemonic](@str nvarchar(4000))
returns nvarchar(4000)
as
begin
declare @zjm varchar(100),@tmp_char varchar(2),@tmp_zjm varchar(2), @i int,@length int
set @zjm =''
set @length = len(@str)
set @i = 1
while @i<=@length
begin
set @tmp_char = substring(@str,@i,1)
select @tmp_zjm =zjm from hz_zjm where hanzi=@tmp_char
if @@rowcount=1
set @zjm = @zjm +Rtrim(@tmp_zjm)
set @i = @i + 1
end
return (@zjm)
end</span>
方法二:存储过程获取;
实现核心代码——SQL存储过程:
<span style="font-size:18px;">-- =============================================
-- Author: zhangbc
-- Create date: 2014-05-24
-- Description: 获取汉字拼音首字母
-- =============================================
CREATE PROCEDURE [dbo].[getMnemonic]
@str varchar(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
select @PY
END</span>
方法三:标量值获取(算法和方法二一样,实现方式不同):
代码如下:
<span style="font-size:18px;">------------------------------------------------
--作者:zhangbc
--时间:2014-03-19
--功能:获取汉字拼音首字母
------------------------------------------------
CREATE function [dbo].[fun_getZjm](@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</span>
方法四:字符编码法;
核心代码如下:
<span style="font-size:18px;">public class getMnemonic
{
public getMnemonic()
{
}
/// <summary>
/// 字符编码的获取
/// </summary>
/// <param name="cnChar">字符</param>
/// <returns>字符编码</returns>
private 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;
}
/// <summary>
/// 字符编码获取助记码
/// </summary>
/// <param name="str">汉字</param>
/// <returns>助记码</returns>
public string getChsSpell(string str)
{
string myStr = "";
for (int i = 0; i < str.Length; i++)
{
myStr += getSpell(str.Substring(i, 1));
}
return myStr;
}
}</span>
测试结果如下:小结:
通过测试结果来看,字符编码(方法四)还是有点不完美,其实通过表获取的方法(方法一)也存在不足,汉字存储太少。
赠送源码一份,供您研究:http://files.cnblogs.com/zhangbc/MnemonicOfWords.rar