Page 1 of 1

【已解决】如何在 ahk unicode 中获取真实的字节长度?

Posted: 18 Sep 2014, 12:41
by tmplinshi
比如 StrLen("1全角") 在 ANSI 中的结果是 5,而 Unicode 中的结果是 3。

我的问题是,如何能在 Unicode 中也获得结果 5?

更新: 找到了一个方法

Code: Select all

MsgBox, % StrLenA("1全角")

StrLenA(string) { ; 注: 不太正确,全角符号不能计算为2
	Loop, Parse, string
		len += ( Asc(A_LoopField) < 0x2E80 or Asc(A_LoopField) > 0x9FFF ) ? 1 : 2
	Return len
}
有更好的方法吗?

Re: 如何在 ahk unicode 中获取真实的字节长度?

Posted: 18 Sep 2014, 19:29
by aamii
试试

Code: Select all

 MsgBox, % StrLen(regexreplace("1全角","[^\x00-\xff]","11"))

Re: 【已解决】如何在 ahk unicode 中获取真实的字节长度?

Posted: 18 Sep 2014, 20:34
by tmplinshi
谢谢,速度快多了!

Re: 【已解决】如何在 ahk unicode 中获取真实的字节长度?

Posted: 18 Sep 2014, 21:40
by tmplinshi

Code: Select all

StrLenA(str) {
	Return DllCall("MSVCRT.DLL\strlen", "AStr", str)
}
经测试,这样更快。

Re: 【已解决】如何在 ahk unicode 中获取真实的字节长度?

Posted: 18 Sep 2014, 23:08
by tmplinshi
lexikos 提供了这个方法,又比上面更快! :mrgreen:

Code: Select all

StrLenA(str) {
	Return StrPut(str, "cp0") - 1
}

Re: 【已解决】如何在 ahk unicode 中获取真实的字节长度?

Posted: 18 Sep 2014, 23:46
by aamii
好方法,学到。
strput那个确快很多