Dinesh
Dinesh

Reputation: 16428

How to escape literal % in format command in Tcl

I came across a situation, where I need to use format in a string which already contains the literal %.
I tried to escape it with backslash. But it didn't help.

set a "test %values text here -- %s"; # That last '%s' is what I wanted to replace
set b "dinesh"
puts [ format $a $b ]

I tried enclosing the string with braces as well. But no use and getting the following error.

--------
bad field specifier "v"
    while executing
"format $a $b "

As you can see, it is trying to format like %v which is not a valid one.

Upvotes: 2

Views: 1503

Answers (1)

Jerry
Jerry

Reputation: 71548

I can't find it anywhere, but you apparently escape the first percentage sign with another percentage sign:

set a "test %%values text here -- %s"
set b "dinesh"
puts [ format $a $b ]
# => test %values text here -- dinesh

Upvotes: 3

Related Questions