今天看到xuejun的一个取汉字首字符的函数,试用了一下,感觉很好用,不敢独享,拿出来跟大家分享:
$PBExportHeader$uf_getfirstletter.srf
$PBExportComments$ 返回给定汉字串的首字母串, xuejun , 19990821
global type uf_getfirstletter from function_object
end type
forward prototypes
global function string uf_getfirstletter (string as_inputstring)
end prototypes
global function string uf_getfirstletter (string as_inputstring);//Function name : uf_GetFirstLetter
//Used to : 返回给定汉字串的首字母串,即声母串
//Input Arguments: as_InputString - string , 给定的汉字串
//Return Value : ls_ReturnString - String , 给定的汉字串的声母串,一律为小写
//Notice : 1. 此方法基于汉字的国标汉字库区位编码的有效性,不符合此编码的
// 系统此函数无效!
// 2. 若汉字串含有非汉字字符,如图形符号或ASCII码,则这些非汉字字符
// 将保持不变.
//Sample : ls_rtn = uf_GetFirstLetter("中华人民共和国")
// ls_rtn will be : zhrmghg
//Scripts:
char lc_FirstLetter[23] //存放国标一级汉字不同读音的起始区位码对应读音
string ls_ch //临时单元
string ls_SecondSecTable //存放所有国标二级汉字读音
string ls_ReturnStr //返回串
integer li_SecPosValue[23] //存放国标一级汉字不同读音的起始区位码
integer i , j
integer li_SectorCode //汉字区码
integer li_PositionCode //汉字位码
integer li_SecPosCode //汉字区位码
integer li_offset //二级字库偏移量
//Set initial value
li_SecPosValue[]={1601,1637,1833,2078,2274,2302,2433,2594,2787,3106,3212,3472,3635,3722,3730,3858,4027,4086,4390,4558,4684,4925,5249 }
lc_FirstLetter[] = {"A", "B","C","D","E","F","G","H","J","K","L","M","N","O","P","Q","R","S","T","W","X","Y","Z"}
ls_SecondSecTable="CJWGNSPGCGNE[Y[BTYYZDXYKYGT[JNNJQMBSGZSCYJSYY[PGKBZGY[YWJKGKLJYWKPJQHY[W[DZLSGMRYPYWWCCKZNKYYGTTNJJNYKKZYTCJNMCYLQLYPYQFQRPZSLWBTGKJFYXJWZLTBNCXJJJJTXDTTSQZYCDXXHGCK[PHFFSS[YBGXLPPBYLL[HLXS[ZM[JHSOJNGHDZQYKLGJHSGQ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GDLQCSEZGXLBLGYXTWMABCHECMWYJYZLLJJYHLG[DJLSLYGKDZPZXJYYZLWCXSZFGWYYDLYHCLJSCMBJHBLYZLYCBLYDPDQYSXQZBYTDKYXJY[CNRJMPDJGKLCLJBCTBJDDBBLBLCZQRPPXJCJLZCSHLTOLJNMDDDLNGKAQHQHJGYKHEZNMSHRP[QQJCHGMFPRXHJGDYCHGHLYRZQLCYQJNZSQTKQJYMSZSWLCFQQQXYFGGYPTQWLMCRNFKKFSYYLQBMQAMMMYXCTPSHCPTXXZZSMPHPSHMCLMLDQFYQXSZYYDYJZZHQPDSZGLSTJBCKBXYQZJSGPSXQZQZRQTBDKYXZKHHGFLBCSMDLDGDZDBLZYYCXNNCSYBZBFGLZZXSWMSCCMQNJQSBDQSJTXXMBLTXZCLZSHZCXRQJGJYLXZFJPHYMZQQYDFQJJLZZNZJCDGZYGCTXMZYSCTLKPHTXHTLBJXJLXSCDQXCBBTJFQZFSLTJBTKQBXXJJLJCHCZDBZJDCZJDCPRNPQCJPFCZLCLZXZDMXMPHJSGZGSZZQLYLWTJPFSYASMCJBTZKYCWMYTCSJJLJCQLWZMALBXYFBPNLSFHTGJWEJJXXGLLJSTGSHJQLZFKCGNNNSZFDEQFHBSAQTGYLBXMMYGSZLDYDQMJJRGBJTKGDHGKBLQKBDMBYLXWCXYTTYBKMRTJZXQJBHLMHMJJZMQASLDCYXYQDLQCAFYWYXQHZ"
//Get it !
ls_ReturnStr = ""
For i=1 to Len(as_InputString) //依次处理as_InputString中每个字符
ls_ch=Mid(as_InputString , i , 1)
If Asc(ls_ch)<128 then // 非汉字
ls_returnStr = ls_returnStr+ls_ch // 不变
Else // 是汉字
ls_ch = Mid(as_InputString , i , 2) // 取出此汉字
li_SectorCode = Asc(Left(ls_ch, 1)) - 160 //区码
li_PositionCode = Asc(Right(ls_ch, 1)) - 160 //位码
li_SecPosCode = li_SectorCode*100 + li_PositionCode // 区位码
If li_SecPosCode>1600 and li_SecPosCode<5590 then // 第一个字符
For j=23 to 1 Step -1 // 找声母
If li_SecPosCode>=li_SecPosValue[j] then
ls_returnStr = ls_returnStr + lc_FirstLetter[j]
Exit
End if
Next
Else // 第一个字符
li_offset = (li_SectorCode - 56 ) *94 + li_PositionCode - 1 // 计算偏移量
If li_offset>=0 and li_offset<=3007 then //二区汉字
ls_returnStr = ls_returnStr + Mid(ls_SecondSecTable, li_offset , 1) //取出此字声母
End if
End if
i = i+1 // 指向下一个汉字
End if
Next // 处理完毕
//Return result
Return lower( ls_returnStr ) //返回 as_InputString 的声母串
end function