如何从“中删除X10”;X4/X2/X10/”的;在TCL?

如何从“中删除X10”;X4/X2/X10/”的;在TCL?,tcl,Tcl,输入为“X4/X2/X10/”。我想从中删除X10。所需输出为“X4/X2/”。最简单的方法是什么?有多种方法。下面是一个将输入转换为片段列表的方法,使用lsearch对该列表进行过滤,然后重新组合结果: set input "X4/X2/X10/" set pieces [split $input "/"] set removed [lsearch -inline -all -not -exact $pieces "X10"] se

输入为“X4/X2/X10/”。我想从中删除X10。所需输出为“X4/X2/”。最简单的方法是什么?

有多种方法。下面是一个将输入转换为片段列表的方法,使用
lsearch
对该列表进行过滤,然后重新组合结果:

set input "X4/X2/X10/"

set pieces [split $input "/"]
set removed [lsearch -inline -all -not -exact $pieces "X10"]
set output [join $removed "/"]

puts $output
我打算删除的最后一个元素不是专门针对X10的

差不多

set input "X4/X2/X10/"
set output [join [lreplace [split $input /] end-1 end-1] /]
puts $output
适用于此。

使用子命令:

set input "X4/X2/X10/"

# find the index of the last slash before the end of string slash
set idx [string last / $input end-1]  ;# => 5

set new [string range $input 0 $idx]  ;# => X4/X2/
或者,一起

set new [string range $input 0 [string last / $input end-1]]

我喜欢glenn的回答,因为当一个人需要一个字符串作为输出时,他就有了使用字符串的精神。类似,考虑使用:


谢谢你的回答。对不起,我的问题不清楚。我打算删除的最后一个元素不是专门针对X10的。对不起,这里是TCL新手
% set input "X4/X2/X10/"
% regexp {(.*/)[^/]+/$} $input _ output 
% set output
X4/X2/