Archive: Concatenate string


Concatenate string
Hi,

howto I can concatenate 2 strings??

Thanks, Lorenzo.


$1 = "one string"
$2 = "second string"

StrCpy $out_var "$1_$2"

$out_var = one string_second string


I need similar thing: I want to show concatenation of strings. I.e.
$1 = "one string,"
$2 = " second string"
MessageBox MB_OK $1+$2
and I want to see "one string, second string"

also I have problems with above-mentioned "code": on line $1 = "one string" I receive error: "Invalid command: $1"...


Where did you get the + idea from?
If you want to join strings together, you exactly do that in the script:

MessageBox MB_OK "$1 $2"
or maybe
MessageBox MB_OK $1$2

Stu


Originally posted by MVI
I need similar thing: I want to show concatenation of strings. I.e.
$1 = "one string,"
$2 = " second string"
MessageBox MB_OK $1+$2
and I want to see "one string, second string"

also I have problems with above-mentioned "code": on line $1 = "one string" I receive error: "Invalid command: $1"...
It appears that you don't know the NSIS scripting language. I suggest you read the manual and look at some of the examples that are included in the NSIS package.

The language does not use = for a string assignment, and does not use + for concatenation. String assignments are done with the command StrCpy. This command is also used for concatenation.

StrCpy $1 "one string"
StrCpy $2 " second string"
MessageBox MB_OK "$1$2"

another method...
StrCpy $1 "one string"
MessageBox MB_OK "$1 second string"

and another...
StrCpy $1 "one string"
StrCpy $1 "$1 second string"
MessageBox MB_OK $1

Don

That all looks good, until you want to concatenate a string literal with an existing string variable, with no separator.

Ex:
I have $R1 with "abc", and want to concatenate "123" to $R1 so that the resulting string is "abc123". The only way I found was :

StrCpy $R1 "abc"
StrCpy "$R1123"

Now that's not readable, is it ? Anyone can share a better solution ? Having some kind of + operator would be useful in such a situation...


StrCpy $R1 "$R1123"

Stu


Yea I forgot a parameter, sorry ...

But you seriously find it readable ? appending a string to a var name like that ? You can't even determine is the var is $R1, $R11, $R12, etc just by looking at teh strcpy line. Think about the person who did not wrote it and needs to maintain it.


Tell him it goes from $R0-$R9 :)

Or,

!macro StrCat _a _b _out
StrCpy ${_out} `${_a}${_b}`
!macroend
!define StrCat `!insertmacro StrCat`
...
${StrCat} abc 123 $R0


Stu

Yea, it's a good idea to define such a macro.

Thanks for the tip :)


Small typo in that code fixed.

Stu