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

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/1430093/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-09 00:32:07  来源:igfitidea点击:

How to concisely concatenate strings in Tcl?

stringtclconcatenation

提问by WilliamKF

I can easily concatenate two variables, foo and bar, as follows in Tcl: "${foo}${bar}".

我可以很容易地连接两个变量,foo 和 bar,在 Tcl 中如下所示:“${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.

似乎“字符串连接”将是我想要的,但它似乎不在我的 Tcl 解释器版本中。

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

String concat is written about here:

字符串 concat 写在这里:

回答by Bryan Oakley

You can embed commands within a double-quoted string without the need for a temporary variable:

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

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

回答by ramanman

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]

回答by SingleNegationElimination

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 将其视为替换后的单个单词,而不管这两个子命令的结果如何。