The task of extracting and manipulating information via a scripting language is often described using the term . Strings are a set of alphanumeric characters stored and manipulated together. These sets are concatenated, truncated, or partitioned by the script with a specific end in mind.
All data items in Tcl, including numeric values, are treated as strings. They are treated as other data types only as needed. This makes string manipulation and the associated commands very important and frequently utilized.
There are a large number of string manipulation commands, as well as pattern and regular expression matching. The following list contains some commonly used string manipulation commands and a summary of their usage. For more in-depth explanations and a complete listing, referred to http://www.tcl.tk/man/ or to a Tcl/Tk handbook.
string compare
Compares if two strings are the same. Returns if equal, if is sorted before , and otherwise.
Returns the length (number of characters) in string.
string match
Uses glob-style matching to determine if a matches . Returns if it matches, otherwise.
string tolower
Converts to all lower case.
string trim
Trims characters from both ends of string. If is not specified, it defaults to whitespace.
append
Concatenates values , etc… onto string.
format
Formats , , etc… according to specific format definitions defined by form.
The string length command can be used to check whether a variable has data.
set var1 {};
string length $var1;
0
set var1 "12345";
string length $var1;
5
Similar to string length, string match is used frequently to verify a variable matches a known value. The command takes two arguments, the pattern to search for and the string or variable.
set axis_label "Nodal Acceleration (m/s^2)";
string match "*Acceleration*" $axis_label;
1
set plot_type xy;
string match abc $plot_type;
0
When outputting data to a file or for display purposes, the format command can be used to make sure the values are correctly formatted.
set number "1.234";
format %f $number;
1.234000
format %e $number;
1.234000e+000
format %5.1f $number;
1.2
format "%-10x%d" Text 100;
Text 100