0

I know a simular question has been asked before here:

How to concatenate a number and a string in auto hotkey

But this question only takes numbers into account. My problem is a bit different. For example:

myStr = A literal string
myInt = 5

Now I wish to concatenate both into a new string: 5A literal string
This is what I've tried so far:

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

It seems like no matter what I try, AHK just can't handle concatenating an integer with a textstring. Has anyone experience with this and know a way to get it working?

EDIT

I should add that I can't solve the issue by doing myInt = "5" as I need to operate on the integer in a loop with myInt++. Also a second question that I haven't figured out yet is: How to add unicode to the string? I thought it was U+0003, but that doesn't seem to work.

EDIT 2

It seems someone else isn't getting the same results. I've updated AHK but the problems remains. So I'll be including my exact code here, perhaps I'm doing something wrong?

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
}

Short explanation: I'm building a little tool that will automaticly colorize text in irc adding a different color to each character. Therefor splitting the string into an array and trying to add:

U:0003ColorNumberCharacter

Where U:0003 should be the unicode for the character used in mIRC with (Ctrl+K).

Community
  • 1
  • 1
icecub
  • 8,615
  • 6
  • 41
  • 70
  • What happens if you put something between the double%? For example, newStr = %myInt%X%myStr% – donjuedo Jun 07 '15 at 03:26
  • @donjuedo Tried that aswell. Same results. Either illegal character or string is converted to some number and added to it. – icecub Jun 07 '15 at 03:36
  • I'm not seeing the same results? In fact it works exactly as it should. I'm thinking you might need to reinstall or update to the latest version of AutoHotkey? Download it at ahkscript.org. – errorseven Jun 07 '15 at 03:53
  • Never mind, got it. Will test – icecub Jun 07 '15 at 03:58
  • @ahkcoder Still the same problem. I've updated my question with my exact code. Perhaps I'm doing something wrong there? – icecub Jun 07 '15 at 04:07
  • Seems like I was simply using the wrong operator.. Feel like an idiot right now – icecub Jun 07 '15 at 04:24

2 Answers2

1

You used

NewText += % ColorNumber thisChar

+ is used for adding up numbers. But the operator for concatenating strings is . in AutoHotkey. Note that this all varies from language to language. So it should be:

NewText .= ColorNumber . thisChar

which is the same as

NewText := NewText . ColorNumber . thisChar

And whenever you use the := operator, there is no need for any % in simple assignments - only when assigning in two steps, e.g., with arrays, like you did correctly with thisChar.

Another way to express the allocation above, with the plain = operator would be

NewText = %NewText%%ColorNumber%%thisChar%

which you figured out yourself already.

phil294
  • 10,038
  • 8
  • 65
  • 98
  • Thanks for your explanation. I did try `.=` before, but that gave me the illegal character error as well. I might have done something wrong there though. Anyway, the script is finished and works perfectly fine :) – icecub Jun 08 '15 at 03:19
0

It turned out I was simply using the wrong operator. The correct code was:

NewText = %NewText%%ColorNumber%%thisChar%
icecub
  • 8,615
  • 6
  • 41
  • 70