Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to concatenate a number and a string in auto hotkey

Tags:

autohotkey

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

like image 725
Graham Avatar asked Jan 27 '14 12:01

Graham


People also ask

How do I concatenate a string and a number?

If you want to concatenate a string and a number, such as an integer int or a floating point float , convert the number to a string with str() and then use the + operator or += operator.

How do I concatenate two characters to a string?

C++ strcat() method The strcat() method is used to concatenate strings in C++. The strcat() function takes char array as input and then concatenates the input values passed to the function.

Can we concatenate string and character?

Concatenating strings would only require a + between the strings, but concatenating chars using + will change the value of the char into ascii and hence giving a numerical output.


1 Answers

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.

like image 61
MCL Avatar answered Oct 26 '22 08:10

MCL