TCL : Concatenate a variable and a string

ⅰ亾dé卋堺 提交于 2019-12-30 05:43:08

问题


Assume we have a variable 'a' set to 12345 :

set a 12345

Now how do i set a new variable 'b' which contains the value of 'a' and another string say 9876

workaround is something like

set a "12345"
set u "9876"

set b $a$u

but i dont want to specify $u instead i want the direct string to used..


回答1:


You can do:

set b ${a}9876

or, assuming b is either set to the empty string or not defined:

append b $a 9876

The call to append is more efficient when $a is long (see append doc).




回答2:


other option is to use set command. since set a gives value of a we can use it to set value of b like below

set b [set a]9876




回答3:


Or,you can use format

set b [format %s%s $a $u]




回答4:


From Tcl 8.6.2 onwards, there is string cat which can be used to solve this problem.

set b [string cat $a 9876]



回答5:


Other option is to use concat command like below.

set b [concat $a\9876]




回答6:


I don't get what you mean the direct string... I'm not sure if you want... However, if you want the value of 12349876 you can do:

% set b [concat $a$u]
12349876

If you want $a or $u to be part of the string, just add a backslash '\' before the desired variable.




回答7:


set myString "Hello"

append myString " World!"

puts "$myString"

Hello World!



来源:https://stackoverflow.com/questions/5241954/tcl-concatenate-a-variable-and-a-string

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!