Tcl Strings
The task of extracting and manipulating information via a scripting language is often described using the term string manipulation. 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 ?-nocase? ?-length len? string1 string2
- Compares if two strings are the same. Returns 0 if equal, -1 if string1 is sorted before string2, and 1 otherwise.
- string length string
- Returns the length (number of characters) in string.
- string match ?-nocase? pattern string
- Uses glob-style matching to determine if a string matches pattern. Returns 1 if it matches, 0 otherwise.
- string tolower string ?first? ?last?
- Converts string to all lower case.
- string trim string ?chars?
- Trims chars characters from both ends of string. If chars is not specified, it defaults to whitespace.
- append string st1 str2 …
- Concatenates values str1, str2 etc… onto string.
- format form str1 str2 …
- Formats str1, str2, etc… according to specific format definitions defined by form.
set var1 {};
string length $var1;
0
set var1 "12345";
string length $var1;
5
set axis_label "Nodal Acceleration (m/s^2)";
string match "*Acceleration*" $axis_label;
1
set plot_type xy;
string match abc $plot_type;
0
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