Locating the index of a character
What if we need to determine which character exists at a specific location within a string and not just the first instance? To accomplish this, string
, includes the index
keyword.
The syntax for the string
command is as follows:
string index string index
When invoked with the index
keyword, the string
command returns the character that exists at the location specified in the switch. The accepted values are valid for all the Tcl commands that accept an index and may be passed as follows:
Value |
Description |
---|---|
Any integer value |
Integer value for a specific index. Please note that the index is 0-based. |
end |
The last character in the string. |
end-n |
The last character in the string minus the numeric offset represented by n. For example, end-2 would refer to "b" in the string "abcd". |
end+n |
The last character in the string plus the numeric offset represented by n. |
A+B |
The character located at the index, as determined by adding the values of A and B, where A... |