【问题标题】:How to concatenate a number and a textstring in auto hotkey如何在自动热键中连接数字和文本字符串
【发布时间】:2015-06-07 03:10:48
【问题描述】:

我知道之前有人问过一个类似的问题:

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。

【问题讨论】:

  • 如果在 double% 之间添加一些东西会发生什么?例如,newStr = %myInt%X%myStr%
  • @donjuedo 也试过了。结果相同。非法字符或字符串都被转换为某个数字并添加到其中。
  • 我没有看到相同的结果?事实上,它完全按照它应该的方式工作。我想您可能需要重新安装或更新到最新版本的 AutoHotkey?在 ahkscript.org 下载。
  • 没关系,明白了。将测试
  • @ahkcoder 还是同样的问题。我已经用我的确切代码更新了我的问题。也许我在那里做错了什么?

标签: autohotkey


【解决方案1】:

你用过

NewText += % ColorNumber thisChar

+ 用于将数字相加。但是 连接字符串 的运算符是 AutoHotkey 中的 .。请注意,这一切都因语言而异。所以应该是:

NewText .= ColorNumber . thisChar

相同
NewText := NewText . ColorNumber . thisChar

无论何时您使用 := 运算符,在简单的赋值中都不需要任何 % - 仅在分两步进行赋值时,例如,使用数组,就像您正确使用 thisChar 所做的那样。

用普通的= 运算符表示上述分配的另一种方式是

NewText = %NewText%%ColorNumber%%thisChar%

你自己已经知道了。

【讨论】:

  • 感谢您的解释。我之前确实尝试过.=,但这也给了我非法字符错误。我可能在那里做错了什么。无论如何,脚本已经完成并且运行良好:)
【解决方案2】:

原来我只是使用了错误的运算符。正确的代码是:

NewText = %NewText%%ColorNumber%%thisChar%

【讨论】:

    猜你喜欢
    • 2014-02-18
    • 2022-07-11
    • 2020-10-17
    • 2012-10-17
    • 1970-01-01
    • 2021-01-19
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多