在 tcl 中加入列表时串联插入额外的 space 字符。有没有办法删除它?
Concatenation inserting additional space character while joining the list in tcl. Is there a way to remove it?
我正在尝试拆分一个变量并希望向第一个字段添加一个后缀,然后使用 tcl
将其与第二个字段连接起来
喜欢
san_jose
到
san_state_jose
我可以拆分代码
set tcl_list [split $var_name "_"]
set new_name [concat [lindex $tcl_list 0] "_state_" [lindex $tcl_list 1]]
puts $new_name
但是在组合它们之前,串联会不幸地插入额外的 space 个字符,如下所示
san _state_ jose
你能帮我更正一下吗?
-问候,
Div
我可以使用 regsub 解决问题,但我也有兴趣学习其他方法。
set new_name [regsub {_} $var_name "_state_"]
puts $new_name
-问候,
D
一种方法是使用 string cat
:
set new_name [string cat [lindex $tcl_list 0] "_state_" [lindex $tcl_list 1]]
或者如果 new_name
不存在,append
:
append new_name [lindex $tcl_list 0] "_state_" [lindex $tcl_list 1]
或者只是很好的旧插值:
set new_name "[lindex $tcl_list 0]_state_[lindex $tcl_list 1]"
或者 linsert
和 join
:
set new_name [join [linsert $tcl_list 1 state] _]
设置tcl_list[拆分san_jose"_"]
设置 new_name [concat [lindex $tcl_list 0]state[lindex $tcl_list 1]]
放 $new_name
我正在尝试拆分一个变量并希望向第一个字段添加一个后缀,然后使用 tcl
将其与第二个字段连接起来喜欢
san_jose
到
san_state_jose
我可以拆分代码
set tcl_list [split $var_name "_"]
set new_name [concat [lindex $tcl_list 0] "_state_" [lindex $tcl_list 1]]
puts $new_name
但是在组合它们之前,串联会不幸地插入额外的 space 个字符,如下所示
san _state_ jose
你能帮我更正一下吗?
-问候, Div
我可以使用 regsub 解决问题,但我也有兴趣学习其他方法。
set new_name [regsub {_} $var_name "_state_"]
puts $new_name
-问候, D
一种方法是使用 string cat
:
set new_name [string cat [lindex $tcl_list 0] "_state_" [lindex $tcl_list 1]]
或者如果 new_name
不存在,append
:
append new_name [lindex $tcl_list 0] "_state_" [lindex $tcl_list 1]
或者只是很好的旧插值:
set new_name "[lindex $tcl_list 0]_state_[lindex $tcl_list 1]"
或者 linsert
和 join
:
set new_name [join [linsert $tcl_list 1 state] _]
设置tcl_list[拆分san_jose"_"] 设置 new_name [concat [lindex $tcl_list 0]state[lindex $tcl_list 1]] 放 $new_name