string map
Replaces each key of a list of keys and values with its value.
string map ?-nocase? MAPPING STRING
Description
MAPPING is a list of keys and values: {key1 value1 key2 value2 ...}. The command reads STRING once, from left to right. At each place it tries the keys in the order of MAPPING, and the first key that starts there goes out as its value. The value is not read again, so string map {a b b a} ab swaps the two letters. When no key starts at a place, its character goes out as it is.
The order of MAPPING matters when one key is a prefix of another: put the longer key first. A key that is an empty string never matches. An empty MAPPING or an empty STRING gives STRING.
Arguments
| Argument | Type | Default | Description |
|---|---|---|---|
| MAPPING | list | required | A list with an even count of elements: each key followed by its value. |
| STRING | string | required | The text to change. |
Options
| Option | Type | Default | Summary |
|---|---|---|---|
-nocase | flag | none | Matches the keys with no regard to the case of ASCII letters. |
-nocase
Matches the keys with no regard to the case of ASCII letters.
Matches a key at a place when the two are equal with no regard to the case of the ASCII letters A to Z. The value goes out as it is in MAPPING. In ztcl the option does not fold a letter outside ASCII: the key é does not match É.
% string map -nocase {ab X} "AB aB"
X X
% string map -nocase {é E} "É"
É% string map -nocase {a} abc
char map list unbalanced
-errorcode NONEReturns
The new string.
Examples
% string map {a 1 b 2} abcab
12c12
% string map {abc 1 ab 2 a 3} abcaba
123
% string map {ab X a Y} aab
YX
% string map {a b b a} ab
ba
% string map {a {}} banana
bnn
Errors
% string map {a} abc
char map list unbalanced
-errorcode NONE
% string map a
wrong # args: should be "string map ?-nocase? charMap string"
-errorcode TCL WRONGARGS
# With three arguments, the first one is an option.
% string map {a 1} abc extra
bad option "a 1": must be -nocase
-errorcode TCL LOOKUP INDEX option
Differences from Tcl
- Tcl 8.6.16 and 9.0.4 fold each letter with a case with
-nocase:string map -nocase {é E} ÉgivesEthere (issue 659).
Facts
- Capability
- None.
- Tcl release
- Tcl 9.1 and 8.6.
- Since
- ztcl 1.0.0-beta.1