Strings

Since strings are so basic in Tcl, there are quite a few commands that make working with them just that little bit easier. This is all from table 9.1 from Ousterhout.

Trimming

Getting rid of unwanted characters surrounding a string:
string trim string ?chars?

Returns a value identical to string except that any leading or trailing character that appear in chars are removed, chars defaults to the white space characters (space, tab, newline, and carriage return).

string trimleft string ?chars?

Same as string trim except that only leading characters are removed.

string trimright string ?chars?

Same as string trim except that only trailing characters are removed.

Case

Changing the case of characters in a string:
string tolower string

Returns a value identical to string except that all uppercase characters have been converted to lowercase.

string toupper string
Returns a value identical to string except that all lowercase characters have been converted to uppercase.

Substring Searching

Looking for one string inside another:
string first string1 string2

Returns the index in string2 of the first character in the leftmost substring that exactly matches string1, or -1 if there is no match.

string last string1 string2

Returns the index in string2 of the first character in the rightmost substring that exactly matches string1, or -1 if there is no match.

Strings as Lists

These commands let you do list things to strings:
string index string charIndex

Returns the charIndex'th character of string, or an empty string if there is no such character. The first character in string has index 0.

string length string

Returns the number of characters in string.

string range string first last

Returns the substring of string that lies between the indeices given by first and last, inclusive. An index of 0 refers to the first character in the string, and last may be end to refer to the last character of the string.

Formatted Input and Output

These commands let you write and read formatted strings:
format formatString ?value value ...?

Returns a result equal to formatString except that the value arguments have been substituted in place of % sequences in formatString.

scan string format varname ?varName varName ...?

Parses fields from string as specified by format and places the values that match % sequences into variable named by the varName arguments. Returns the number of fields successfully parsed.

Matching and Splitting

Finding out which parts of a string match a pattern:
regexp ?-indices? ?-nocase? ?--? exp string ?matchVar? ?subVar subVar ...?

Determines whether the regular expression exp matches part of all of string and returns 1 it id does, 0 if it doesn't. If there is a match, information about matching range(s) is placed in the variables named by matchVar and the subVar's, if they are specified.

regsub ?-all? ?-nocase? ?--? exp string subSpec varName

Matches exp against string as for regexp and returns 1 if there is a match, 0 if there is none. Also copies string into the variable named by varName, making substitutions for the matching portions(s) as specified by subSpec

string compare string1 string2

Returns -1, 0, or 1 if string1 is lexicographically less then, equal to, or greater than string2.

string match pattern string

Returns 1 if pattern matches string using glob-style matching rules (*, ?, [], and \) and 0 if it doesn't.


Previous Contents Next
Clinton Roy