6
set some_string "Name/is/ComplexSTRUCTUre" 

convert this string to,

some_string = "Name/is/ComplexSTR.CTUre" 

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

toxic_boi_8041
  • 1,424
  • 16
  • 26
Harshad_Dinfi
  • 89
  • 1
  • 1
  • 6

3 Answers3

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

Community
  • 1
  • 1
toxic_boi_8041
  • 1,424
  • 16
  • 26
5

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.

Mikhail T.
  • 3,043
  • 3
  • 29
  • 46
  • 1
    `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 – toxic_boi_8041 May 08 '16 at 06:16
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>
morgan121
  • 2,213
  • 1
  • 15
  • 33