Harshad_Dinfi
Harshad_Dinfi

Reputation: 99

How to replace particular character in string with tcl script?

set some_string "Name/is/ComplexSTRUCTUre" 

convert this string to,

some_string = "Name/is/ComplexSTR.CTUre" 

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

Upvotes: 7

Views: 57810

Answers (3)

Lokeshajju
Lokeshajju

Reputation: 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>

Upvotes: 0

Mikhail T.
Mikhail T.

Reputation: 3967

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.

Upvotes: 6

toxic_boi_8041
toxic_boi_8041

Reputation: 1482

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

Upvotes: 11

Related Questions