How to replace particular character in string with tcl script?

May 2024 ยท 1 minute read
set some_string "Name/is/ComplexSTRUCTUre" 

convert this string to,

some_string = "Name/is/ComplexSTR.CTUre" 

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

3 Answers

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"

More information can be found here tcl string replacement

0

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

set s "Name/is/ComplexSTRUCTUre" set s [string map {U .} $s] 

This, however, will replace all Us with dots -- as your question's title suggests.

If, however, you want only the first U replaced -- as the text of your question implies, then AxT_8041's second option is the most suitable.

1

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> 

ncG1vNJzZmirpJawrLvVnqmfpJ%2Bse6S7zGiorp2jqbawutJoam9xaGiGeH%2BOoaawZaSkerOxz6WYnJ1dpa6zwMicrKWZomKwqa3RmpqtnaJitq950q2poqaXYsSqwMdmq5ykXaiws7XPrQ%3D%3D