如何在 autohotkey 中连接数字和文本字符串
How to concatenate a number and a textstring in auto hotkey
我知道之前有人在这里问过一个类似的问题:
How to concatenate a number and a string in auto hotkey
但是这道题只考虑了数字。我的问题有点不同。例如:
myStr = A literal string
myInt = 5
现在我希望将两者连接成一个新字符串:5A literal string
这是我到目前为止尝试过的:
newStr = %myInt%%myStr% ;Result: Error illegal character found
newStr = % myInt myStr ;Result: Some number
convertString = (%myInt% . String)
newStr = %convertString%%myStr% ;Result: Error illegal character found
似乎无论我尝试什么,AHK 都无法处理将整数与文本字符串连接起来的问题。有没有人有这方面的经验并且知道如何让它工作?
编辑
我应该补充一点,我无法通过执行 myInt = "5"
来解决问题,因为我需要使用 myInt++
在循环中对整数进行运算。还有第二个我还没有弄清楚的问题是:如何将 unicode 添加到字符串中?我以为是 U+0003
,但好像不行。
编辑 2
其他人似乎没有得到相同的结果。我已经更新了 AHK,但问题仍然存在。所以我会在这里包含我的确切代码,也许我做错了什么?
global OriText ;Contains textstring
global NewText ;Empty
global ColorNumber
ColorNumber = 2
convert_text(){
StringSplit, char_array, OriText
Loop, %char_array0%
{
thisChar := char_array%a_index%
NewText += % ColorNumber thisChar
MsgBox, %NewText%
ColorNumber++
if (ColorNumber = 13){
ColorNumber = 2
}
}
GuiControl,, NewText, %NewText%
ColorNumber = 2
}
简短说明:我正在构建一个小工具,可以自动为 irc 中的文本着色,为每个字符添加不同的颜色。因此将字符串拆分为一个数组并尝试添加:
U:0003ColorNumberCharacter
其中 U:0003 应该是 mIRC 中使用 (Ctrl+K) 的字符的 unicode。
原来我只是用错了运算符。正确的代码是:
NewText = %NewText%%ColorNumber%%thisChar%
你用过
NewText += % ColorNumber thisChar
+
用于累加个数。但是 连接字符串 的运算符在 AutoHotkey 中是 .
。请注意,这一切因语言而异。所以应该是:
NewText .= ColorNumber . thisChar
与
相同
NewText := NewText . ColorNumber . thisChar
并且每当您使用 :=
运算符时,在简单赋值中不需要任何 %
- 只有在分两步赋值时,例如,使用数组,就像您正确地使用 thisChar
.
另一种表达上述分配的方式,使用普通的 =
运算符是
NewText = %NewText%%ColorNumber%%thisChar%
你已经自己想出来了。
我知道之前有人在这里问过一个类似的问题:
How to concatenate a number and a string in auto hotkey
但是这道题只考虑了数字。我的问题有点不同。例如:
myStr = A literal string
myInt = 5
现在我希望将两者连接成一个新字符串:5A literal string
这是我到目前为止尝试过的:
newStr = %myInt%%myStr% ;Result: Error illegal character found
newStr = % myInt myStr ;Result: Some number
convertString = (%myInt% . String)
newStr = %convertString%%myStr% ;Result: Error illegal character found
似乎无论我尝试什么,AHK 都无法处理将整数与文本字符串连接起来的问题。有没有人有这方面的经验并且知道如何让它工作?
编辑
我应该补充一点,我无法通过执行 myInt = "5"
来解决问题,因为我需要使用 myInt++
在循环中对整数进行运算。还有第二个我还没有弄清楚的问题是:如何将 unicode 添加到字符串中?我以为是 U+0003
,但好像不行。
编辑 2
其他人似乎没有得到相同的结果。我已经更新了 AHK,但问题仍然存在。所以我会在这里包含我的确切代码,也许我做错了什么?
global OriText ;Contains textstring
global NewText ;Empty
global ColorNumber
ColorNumber = 2
convert_text(){
StringSplit, char_array, OriText
Loop, %char_array0%
{
thisChar := char_array%a_index%
NewText += % ColorNumber thisChar
MsgBox, %NewText%
ColorNumber++
if (ColorNumber = 13){
ColorNumber = 2
}
}
GuiControl,, NewText, %NewText%
ColorNumber = 2
}
简短说明:我正在构建一个小工具,可以自动为 irc 中的文本着色,为每个字符添加不同的颜色。因此将字符串拆分为一个数组并尝试添加:
U:0003ColorNumberCharacter
其中 U:0003 应该是 mIRC 中使用 (Ctrl+K) 的字符的 unicode。
原来我只是用错了运算符。正确的代码是:
NewText = %NewText%%ColorNumber%%thisChar%
你用过
NewText += % ColorNumber thisChar
+
用于累加个数。但是 连接字符串 的运算符在 AutoHotkey 中是 .
。请注意,这一切因语言而异。所以应该是:
NewText .= ColorNumber . thisChar
与
相同NewText := NewText . ColorNumber . thisChar
并且每当您使用 :=
运算符时,在简单赋值中不需要任何 %
- 只有在分两步赋值时,例如,使用数组,就像您正确地使用 thisChar
.
另一种表达上述分配的方式,使用普通的 =
运算符是
NewText = %NewText%%ColorNumber%%thisChar%
你已经自己想出来了。