string 如何用tcl脚本替换字符串中的特定字符?

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/36983973/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-08 16:32:07  来源:igfitidea点击:

How to replace particular character in string with tcl script?

stringtclcharacter-replacement

提问by Harshad_Dinfi

set some_string "Name/is/ComplexSTRUCTUre" 

convert this string to,

将此字符串转换为,

some_string = "Name/is/ComplexSTR.CTUre" 

i.e replacing first "U"to "."

即首先替换"U""."

回答by mf_starboi_8041

Try This,

尝试这个,

set replaced_string [regsub "U" $some_string "."]
puts $replaced_string

Another Option,

另外一个选项,

set pos [string first "U" $some_string]
set replaced_string [string replace $some_string $pos $pos "."]
puts $replaced_string

Here your "Name/is"portion should not contain any "U"

在这里,您的"Name/is"部分不应包含任何"U"

More information can be found here tcl string replacement

更多信息可以在这里找到tcl 字符串替换

回答by Mikhail T.

Use of regsubis overkill for plain strings. If you simply wish to replace one set of substrings with another, string mapis your friend:

regsub对于普通字符串,使用 of是矫枉过正。如果您只是想用另一组子字符串替换一组子字符串,那么string map您的朋友是:

set s "Name/is/ComplexSTRUCTUre"

set s [string map {U .} $s]

This, however, will replace allUs with dots -- as your question's titlesuggests.

但是,这将用点替换所有我们 - 正如您的问题标题所暗示的那样。

If, however, you want only the firstU replaced -- as the textof your question implies, then AxT_8041's second option is the most suitable.

但是,如果您只想替换第一个U - 正如您的问题文本所暗示的那样,那么 AxT_8041 的第二个选项是最合适的。

回答by Lokeshajju

you can use:

您可以使用:

string replace <num1> <num2> <To_be_replaced>

example:

例子:

set a [get_attr [get_cells -filter <cell_name> ] ref_name]
<cell_name>

string replace 5 8 NAME
<cell_NAME>