string
Perform various operations on strings.
Syntax
tcl
string subcommand arg ?arg ...?Subcommands
| Subcommand | Description |
|---|---|
string length string | Returns the number of characters in the string |
string index string charIndex | Returns the character at the specified index |
string range string first last | Returns a substring from first to last index |
string match ?-nocase? pattern string | Returns 1 if string matches the glob pattern |
string toupper string | Converts to uppercase |
string tolower string | Converts to lowercase |
string totitle string ?first? ?last? | Converts to title case (optionally only a range) |
string trim string ?chars? | Removes characters from both ends |
string trimleft string ?chars? | Removes characters from the left |
string trimright string ?chars? | Removes characters from the right |
string map ?-nocase? mapping string | Replaces substrings according to mapping |
string cat ?string1? ?string2...? | Concatenates strings |
string compare ?-nocase? ?-length len? string1 string2 | Lexicographic comparison returning -1, 0, or 1 |
string equal ?-nocase? ?-length len? string1 string2 | Equality test returning 0 or 1 |
string first needleString haystackString ?startIndex? | Find first occurrence of substring |
string last needleString haystackString ?lastIndex? | Find last occurrence of substring |
string repeat string count | Repeat string N times |
string reverse string | Reverse character order |
string insert string index insertString | Insert substring at index |
string replace string first last ?newstring? | Replace range with optional new string |
string is class ?-strict? ?-failindex var? string | Character/value class testing |
Parameters
- string: The string to operate on
- charIndex: Zero-based character position (or
end,end-N) - first, last: Range indices for substring operations
- pattern: Glob pattern with
*,?, and[chars]wildcards - chars: Characters to trim (defaults to whitespace)
- mapping: List of old/new pairs for replacement
- class: Character or value class for
string is(see below)
Supported string is Classes
Character classes: alnum, alpha, ascii, control, digit, graph, lower, print, punct, space, upper, wordchar, xdigit
Value classes: boolean, true, false, integer, double, list, dict
Options:
-strict- Empty string returns false (default: empty returns true for character classes)-failindex varname- Sets variable to index of first non-matching character
Examples
string length
Output
string index
Output
string range
Output
string match
Output
string toupper / tolower / totitle
Output
string trim
Output
string map
Output
string cat
Output
string compare / equal
Output
string first / last
Output
string repeat / reverse
Output
string insert / replace
Output
string is
Output
Notes
string toupperandstring tolowerconvert the entire string; the optional?first? ?last?range arguments are accepted but ignored in the current implementation.string issupports both character classes (alnum, alpha, etc.) and value classes (integer, double, list, dict, boolean).
