【发布时间】:2016-08-20 20:40:07
【问题描述】:
tcl 中有一个命令允许在现有的基础上通过替换创建新字符串:
string map ?-nocase? mapping string
tcl 中是否有允许更改现有字符串的命令? 例如:
set string1 abcabcabc
通过使用一些命令将字母“a”替换为“0”,得到值为0bc0bc0bc的string1
【问题讨论】:
标签: string dictionary replace tcl
tcl 中有一个命令允许在现有的基础上通过替换创建新字符串:
string map ?-nocase? mapping string
tcl 中是否有允许更改现有字符串的命令? 例如:
set string1 abcabcabc
通过使用一些命令将字母“a”替换为“0”,得到值为0bc0bc0bc的string1
【问题讨论】:
标签: string dictionary replace tcl
尽管@peter 解决方案运行良好。
要替换文件TCL 中的string 而不使用fileutil 包:
set fd [open "filename.txt" r]
set newfd [open "filename.txt.tmp" w]
while {[gets $fd line] >= 0} {
set newline [string map {a 0} $line]
puts $newfd $newline
}
close $fd
close $newfd
file rename -force "filename.txt.tmp" "filename.txt"
【讨论】:
set f [open file.txt r+] ; puts -nonewline $f [string map {a 0} [read $f]][seek $f 0;list] ; close $f ;) 也可以,但我不建议将它用于生产代码。