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

遇到了问题?请先进行搜索(中文和英文),然后在此提问

Moderators: tmplinshi, arcticir

tmplinshi
Posts: 1604
Joined: 01 Oct 2013, 14:57

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

18 Sep 2014, 12:41

比如 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
}
有更好的方法吗?
Last edited by tmplinshi on 18 Sep 2014, 20:33, edited 1 time in total.
aamii
Posts: 47
Joined: 23 May 2014, 03:50

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

18 Sep 2014, 19:29

试试

Code: Select all

 MsgBox, % StrLen(regexreplace("1全角","[^\x00-\xff]","11"))
tmplinshi
Posts: 1604
Joined: 01 Oct 2013, 14:57

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

18 Sep 2014, 21:40

Code: Select all

StrLenA(str) {
	Return DllCall("MSVCRT.DLL\strlen", "AStr", str)
}
经测试,这样更快。
Last edited by tmplinshi on 18 Sep 2014, 23:15, edited 1 time in total.
tmplinshi
Posts: 1604
Joined: 01 Oct 2013, 14:57

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

18 Sep 2014, 23:08

lexikos 提供了这个方法,又比上面更快! :mrgreen:

Code: Select all

StrLenA(str) {
	Return StrPut(str, "cp0") - 1
}
aamii
Posts: 47
Joined: 23 May 2014, 03:50

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

18 Sep 2014, 23:46

好方法,学到。
strput那个确快很多

Return to “请求帮助”

Who is online

Users browsing this forum: No registered users and 16 guests