看起来像一个基本任务,但我想要一个以某种东西结尾的字符串,并用其他东西替换最后3个字母。这里是我的尝试:正确操纵TCL中的字符串
set v "this is bob"
set index2 [string length $v]
set index1 [expr $index2 - 3]
set result [string replace $v index1 index2 dog]
puts $result; # I want it to now say "this is dog"
编辑:忘了提,它给我的错误信息是:
bad index "index1": must be integer?[+-]integer? or end?[+-]integer? while executing "string replace $v index1 index2 .hdr" invoked from within "set result [string replace $v index1 index2 .hdr]" (file "string_manipulation.tcl" line 7)
只是为了好奇,你为什么假设最后一个单词总是3个字母? – Varun
由于我将使用文件扩展名.am,并且我想用.hdr替换它以保存为不同的输出。 – elefun