How to concatenate a number and a string in auto hotkey

拥有回忆 提交于 2019-12-21 07:34:04

问题


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"

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

Just updated to latest version 1.1.14.01 and its still the same


回答1:


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. The mathematical approach marks the variables as numbers, leading to possible trailing zeros, which you may want to Round() before displaying. The output of our string concatenation can be used as a number as well, since AHK auto-boxes them if neccessary. For example, you could calculate
z := str1 - 1
and it would evaluate to 56.
I personally prefer the mathematical approach, since it will result result in an actual number and not a string, which seems only logical.



来源:https://stackoverflow.com/questions/21381300/how-to-concatenate-a-number-and-a-string-in-auto-hotkey

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!