How to Replace Particular Character in String with Tcl Script?

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 "."

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>

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge that you have read and understand our privacy policy and code of conduct.

Marcus Vance
Author

Marcus Vance

Marcus Vance is a cybersecurity auditor and technology writer dedicated to educating the public about online safety, data privacy regulations, enterprise security, and emerging cyber threats.