/*
默认的strlen只能统计常规字符串数量,DllCall与StrPut都是,且多数扩展字符占用都大于两个字节。
可以用正则来更精确地统计
*/
str:="长度?" ;2个基本汉字+1个unicode扩展G汉字
len:=GetStrLen(str,obj) ;返回长度与切割后的数组obj
MsgBox % "StrLen:" StrLen(str) "`t`tGetStrLen:" len
MsgBox % "如果不包含中文字符直接返回`n拼音首字母:" GetFirstChar("如果不包含中文字符直接返回")
GetStrLen(str,ByRef obj:="") {
Static Count:=0
str:=RegExReplace(str,"[\s\t\r\n]")
obj:=StrSplit(RTrim(RegExReplace(str,"O)(.)","$1☯☯☯",Count),"☯☯☯"),"☯☯☯")
Return Count
}
;;获取中文字符串拼音首字母
GetFirstChar(String){
static nothing := VarSetCapacity(var, 2)
static FirstChar:=""
static array := [ [-20319,-20284,"A"], [-20283,-19776,"B"], [-19775,-19219,"C"], [-19218,-18711,"D"]
, [-18710,-18527,"E"], [-18526,-18240,"F"], [-18239,-17923,"G"], [-17922,-17418,"H"]
, [-17417,-16475,"J"], [-16474,-16213,"K"], [-16212,-15641,"L"], [-15640,-15166,"M"]
, [-15165,-14923,"N"], [-14922,-14915,"O"], [-14914,-14631,"P"], [-14630,-14150,"Q"]
, [-14149,-14091,"R"], [-14090,-13319,"S"], [-13318,-12839,"T"], [-12838,-12557,"W"]
, [-12556,-11848,"X"], [-11847,-11056,"Y"], [-11055,-10247,"Z"] ]
if !RegExMatch(String, "[^\x{00}-\x{ff}]")
Return String
obj:=StrSplit(RTrim(RegExReplace(String,"O)(.)","$1☯☯☯",Count),"☯☯☯"),"☯☯☯")
Loop, % Count
{
if ( Asc(obj[A_Index]) >= 0x2E80 and Asc(obj[A_Index]) <= 0x9FFF )
{
StrPut(obj[A_Index], &var, "CP936"),nGBKCode :=(NumGet(var, 0, "UChar") << 8)+NumGet(var, 1, "UChar")-65536
For Section, element in array
{
if nGBKCode between % element.1 and % element.2
{
FirstChar .= element.3
Break
}
}
}else
FirstChar .= obj[A_Index]
}
Return FirstChar
}
谢谢大神分享?,学习