string TCL 字符串连接

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/5908496/
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 01:05:26  来源:igfitidea点击:

TCL string concat

stringtclconcatenationconcat

提问by Narek

What is the recommended way of concatenation of strings?

推荐的字符串连接方式是什么?

回答by Donal Fellows

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).

Tcl 将字符串连接作为基本操作;它甚至没有真正的语法,因为您只是将字符串彼此相邻编写(或生成它们的变量替换)。

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 appendcommand; it's faster because it uses an intelligent memory management pattern behind the scenes.

最后,如果您要在变量末尾添加字符串,请使用以下append命令;它更快,因为它在幕后使用了智能内存管理模式。

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

回答by TrojanName

Use append.

使用append

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

回答by LaC

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

如果它们包含在变量中,您可以简单地编写"$a$b".