简体   繁体   English

如何在Tcl中简洁地连接字符串?

[英]How to concisely concatenate strings in Tcl?

I can easily concatenate two variables, foo and bar, as follows in Tcl: "${foo}${bar}". 我可以很容易地连接两个变量foo和bar,如下所示:“$ {foo} $ {bar}”。

However, if I don't want to put an intermediate result into a variable, how can I easily concatenate the results of calling some proc? 但是,如果我不想将中间结果放入变量中,如何轻松连接调用某些proc的结果?

Long hand this would be written: 这将写得很长:

set foo [myFoo $arg]
set bar [myBar $arg]
set result "${foo}${bar}"

Is there some way to create result without introducing the temporary variables foo and bar? 有没有办法在不引入临时变量foo和bar的情况下创建结果?

Doing this is incorrect for my purposes: 这样做对我的目的不正确:

concat [myFoo $arg] [myBar $arg]

as it introduces a space between the two results (for list purposes) if one does not exist. 因为它在两个结果之间引入了一个空格(用于列表目的),如果不存在的话。

Seems like 'string concat' would be what I want, but it does not appear to be in my version of Tcl interpreter. 看起来像'string concat'将是我想要的,但它似乎不在我的Tcl解释器版本中。

string concat [myFoo $arg] [myBar $arg]

String concat is written about here: 字符串连接在这里写:

您可以在双引号字符串中嵌入命令,而无需临时变量:

set result "[myFoo $arg][myBar $arg]"

If you are doing this many times, in a loop, or separated by some intermediate code, you might also consider: 如果您多次执行此操作,循环执行或由某些中间代码分隔,您可能还会考虑:

set result ""
append result [myFoo $arg]
append result [myBar $arg]
append result [myBaz $arg]

just write it as a word with no extra spaces: 只是把它写成一个没有多余空格的单词:

[myFoo $arg][myBar $arg]

Tcl sees this as a single word after substitution, regardless of the result of the two subcommands. 无论两个子命令的结果如何,Tcl在替换后将其视为单个单词。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM