Skip to content

string ​

Perform various operations on strings.

Syntax ​

tcl
string subcommand arg ?arg ...?

Subcommands ​

SubcommandDescription
string length stringReturns the number of characters in the string
string index string charIndexReturns the character at the specified index
string range string first lastReturns a substring from first to last index
string match ?-nocase? pattern stringReturns 1 if string matches the glob pattern
string toupper stringConverts to uppercase
string tolower stringConverts 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 stringReplaces substrings according to mapping
string cat ?string1? ?string2...?Concatenates strings
string compare ?-nocase? ?-length len? string1 string2Lexicographic comparison returning -1, 0, or 1
string equal ?-nocase? ?-length len? string1 string2Equality 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 countRepeat string N times
string reverse stringReverse character order
string insert string index insertStringInsert substring at index
string replace string first last ?newstring?Replace range with optional new string
string is class ?-strict? ?-failindex var? stringCharacter/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 toupper and string tolower convert the entire string; the optional ?first? ?last? range arguments are accepted but ignored in the current implementation.
  • string is supports both character classes (alnum, alpha, etc.) and value classes (integer, double, list, dict, boolean).

See Also ​

  • concat - Concatenate strings
  • join - Join list elements
  • split - Split string into list
  • format - Format strings