Returning a range of characters from a string
It is not uncommon to cut a string into its constituent parts. This allows the data to be stored in-line, without comma or space separation, thereby, minimizing the impact on the disk storage. To assist us in this, and other instances where we need to extract a range of characters from a string, Tcl has provided the range
keyword.
The syntax of the string
command is as follows:
string range string first last
When invoked with the range
keyword, the string
command will return all characters inclusive between those specified in the first
and last
location.
How to do it…
In the following example we will locate a range of characters contained within a string. Return values from the commands are provided for clarity. Enter the following command:
% string range abcdefg 2 4 cde
How it works…
As you can see, Tcl has returned the characters "cde", based on the index values passed. The string
command returns a range of consecutive characters from the string...