假设我有一个字符串wordone wordtwo wordthird。我不知道 wordtwo 是什么,但只要它存在就需要将其删除。我该怎么做才能将其删除。
我尝试使用 lreplace $string 1 1 "" 但收到错误“大括号中的列表元素后跟...而不是空格”
如果您将字符串视为列表,您可以使用 lreplace 删除第二个单词,例如
set input_string "wordone wordtwo wordthree"
set result [lreplace $input_string 1 1]
输出:
wordone wordthree
再多思考一下,最好使用 regsub 来匹配空白,并提取第二个单词。这应该有效:
set input_string "wordone wordtwo wordthree"
regsub {^(\S+)\s+\S+} $input_string {\1} result
puts $result ;# wordone wordthree