How to concatenate a number and a string in auto hotkey

前端 未结 1 2006
渐次进展
渐次进展 2021-02-13 04:10

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 \"

相关标签:
1条回答
  • 2021-02-13 04:37

    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.

    0 讨论(0)
提交回复
热议问题