简体   繁体   English

如何在自动热键中连接数字和字符串

[英]How to concatenate a number and a string in auto hotkey

I have the following auto hotkey script: 我有以下自动热键脚本:

A:= 5
B := "7"
C := A.B
MsgBox %C%

The third line does not work. 第三行不起作用。

I'm expecting output of "57" 我期待输出“57”

I have tried the following: 我尝试过以下方法:

C := %A%.%B%
C := (A).(B)
C := (A.B)
C := (%A%.%B%)
C := (%A%).(%B%)

None of which work 这些都不起作用

Can anyone tell me how to do it? 谁能告诉我怎么做?

I'm using version 1.1.09.04 我使用的是1.1.09.04版

Just updated to latest version 1.1.14.01 and its still the same 刚刚更新到最新版本1.1.14.01,它仍然是相同的

You have distinguish between expressions ( := ) and "normal" value assigments ( = ). 您区分了表达式( := )和“正常”值分配( = )。 Your goal can be met with several approaches, as shown in the following examples: 您可以通过多种方法实现目标,如以下示例所示:

a := 5
b := 7
x := 6789

; String concatenation
str1 = %a%%b%
; or as an expression
str2 := a b
; or with explicit concatenation operators
str3 := a . b

; Mathematical "concatenation"

; if b has exactly one digit
val1 := a*10 + b
; for any integer
val2 := a * (10**StrLen(x)) + x ; ** is the "power" operator

msgbox, str1 = %str1%`nstr2 = %str2%`nstr3 = %str3%`nval1 = %val1%`nval2 = %val2%

This code will print: 此代码将打印:

str1 = 57
str2 = 57
str3 = 57
val1 = 57
val2 = 56789

In AHK, all of these methods should be quasi-equivalent: They produce the same kind of output. 在AHK中,所有这些方法都应该是准等价的:它们产生相同类型的输出。 The mathematical approach marks the variables as numbers, leading to possible trailing zeros, which you may want to Round() before displaying. 数学方法将变量标记为数字,导致可能的尾随零,您可能希望在显示之前使用Round() The output of our string concatenation can be used as a number as well, since AHK auto-boxes them if neccessary. 我们的字符串连接的输出也可以用作数字,因为如果需要,AHK会自动将它们装箱。 For example, you could calculate 例如,你可以计算
z := str1 - 1
and it would evaluate to 56 . 它将评估为56
I personally prefer the mathematical approach, since it will result result in an actual number and not a string, which seems only logical. 我个人更喜欢数学方法,因为它会导致实际数字而不是字符串,这似乎是合乎逻辑的。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM