Replacing values within a string
Tcl has added a very useful keyword to the string command to simplify alteration of the string. The map
keyword allows us to replace values within a string as passed without having to programmatically locate the target characters.
The syntax of the string
command is as follows:
string map -nocase mapping string
The string
command will read the mapping provided and replace the affected values within the string. Mapping is passed as a valid Tcl list in a key-value pair format similar to that returned by performing a get
on an array. Bear in mind that the string is only mapped once. If the optional nocase
switch is provided, a case insensitive match will be made.
How to do it…
In the following example, we will replace ...
Get Tcl/Tk 8.5 Programming Cookbook now with the O’Reilly learning platform.
O’Reilly members experience books, live events, courses curated by job role, and more from O’Reilly and nearly 200 top publishers.