TCL Strings
Because strings are the most prevalent data type in Tcl, it makes sense that Tcl provides a rich set of functions for manipulating them. Most
string operations are done by means of the string command, which takes the following general form:
string option arg ?arg ...?
The string command actually performs several different functions, and the option argument
is used to differentiate between them. Example 8.1 creates a string and then uses the string command to manipulate it, and obtain information about it.
Example 8.1:
set str "This is a string" puts "The string is: $str" puts "The length of the string is: [string length $str]" puts "The character at index 3 is: [string index $str 3]" puts "The characters from index 4 through 8 are: [string range $str 4 8]" puts "The index of the first occurrence of letter \"i\" is: [string first i $str]"
Output:
The string is: This is a string
The length of the string is: 16
The character at index 3 is: s
The characters from index 4 through 8 are: is a
The index of the first occurrence of letter "i" is: 2
In Example 8.1, a variable called "str" is created, and initialized to the value, "This is a string". The string
command is then used with various options to obtain various pieces of information about the string. Refer to the manual
page for the string command for a complete listing and explanation of the various options. Also, there
are several other string-related commands worth exploring, such as format, regexp,
regsub, and scan.