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.
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
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, starting with the index value stored in first
and ending with the value stored in last
. An index of 0
refers to the first character. If first
is a negative number or last
is greater than the string length, then Tcl will adjust them to meet the actual start and end of the string.
3.141.42.116