输入是"X4/X2/X10/“。我想从这里删除X10。所需的输出是"X4/X2/“。做这件事最简单的方法是什么?
发布于 2020-12-17 06:19:20
使用string子命令:
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]]发布于 2020-12-16 18:50:29
有很多种方法。下面的示例将输入转换为一个片段列表,使用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发布于 2020-12-16 20:22:13
我的目的是删除最后一个不是专门针对X10的元素
就像这样
set input "X4/X2/X10/"
set output [join [lreplace [split $input /] end-1 end-1] /]
puts $output这很管用。
https://stackoverflow.com/questions/65321455
复制相似问题