7
set some_string "Name/is/ComplexSTRUCTUre" 

convert this string to,

some_string = "Name/is/ComplexSTR.CTUre" 

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

3 Answers 3

11

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

Sign up to request clarification or add additional context in comments.

Comments

6

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 Comment

string map don't allows user to change only first occurrence. it changes every occurrence of U with dot which is not OP's need. Still if you want change all occurrence thn string map is nice and sweet option
0

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>

Comments

Your Answer

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

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.