Narek
Narek

Reputation: 39891

TCL string concat

What is the recommended way of concatenation of strings?

Upvotes: 29

Views: 121869

Answers (4)

Sam Bromley
Sam Bromley

Reputation: 83

These days, you can concatenate two literal strings using the "string cat" command:

set newstring [string cat "string1" "string2"]

Upvotes: 3

TrojanName
TrojanName

Reputation: 5375

Use append.

set result "The result is "
append result "Earth 2, Mars 0"

Upvotes: 32

Donal Fellows
Donal Fellows

Reputation: 137787

Tcl does concatenation of strings as a fundamental operation; there's not really even syntax for it because you just write the strings next to each other (or the variable substitutions that produce them).

set combined $a$b

If you're doing concatenation of a variable's contents with a literal string, it can be helpful to put braces around the variable name or the whole thing in double quotes. Or both:

set combined "$a${b}c d"

Finally, if you're adding a string onto the end of a variable, use the append command; it's faster because it uses an intelligent memory management pattern behind the scenes.

append combined $e $f $g
# Which is the same as this:
set combined "$combined$e$f$g"

Upvotes: 51

LaC
LaC

Reputation: 12824

If they are contained in variables, you can simply write "$a$b".

Upvotes: 8

Related Questions