Manipulating Strings

The string Command

The string command has a number of subcommands for comparing strings, searching for characters and substrings, extracting parts of strings, converting case and the like.

The string length Command

string length string
Returns the length of string (in bytes).

Comparing Strings

string compare string1 string2
string match pattern string
string compare compares its two arguments and returns 0 if they are equal, -1 if string1 is lexicographically less than string2, or 1 otherwise.

string match returns 1 if string matches pattern, 0 otherwise. The pattern matching is done via globbing.

Extracting Substrings

string first string1 string2
string last string1 string2
string index string charIndex
string range string first last
string first returns the index of the leftmost occurrence string1 in string2, or -1 if it's not found. string last returns the index of the rightmost occurrence. Matching is exact, and the index is that of the first character of string1.

string index extracts the charIndex'th character of string.

string range extracts a substring of string from first to last inclusive. The keyword end may be used for last.

Converting Case

string tolower string
string toupper string
These commands convert string to lowercase or uppercase, respectively.

Trimming Strings

string trim string ?chars?
string trimleft string ?chars?
string trimright string ?chars?
The trim commands remove leading (trimleft), trailing (trimright) whitespace -- or both (string trim). Whitespace is defined as the default value of chars: " \t\n\r"; you can override this default with any trim characters you like.

Extended Tcl Substring Extraction Commands

csubstr string firstExpr lengthExpr
ctoken strvar separators

The cexpand Command

Extended Tcl:
cexpand string

The ctype Command

Extended Tcl:
ctype ?-failindex var? class string

The replicate Command

Extended Tcl:
replicate string countExpr

The translit Command

Extended Tcl:
translit inrange outrange string

The for\&mat Command

The scan Command

Manipulating Dates

Extended Tcl

The fmtclock Command

fmtclock clockval ?for\&mat? ?GMT|{}?
Converts a Unix integer time value, typically returned by getclock, convertclock, or the atime, mtime, or ctime options of the file command, to human-readable form. The for\&mat argument is a string that describes how the date and time are to be formatted. Field descriptors consist of a "%" followed by a field descriptor character. All other characters are copied into the result. Valid field descriptors are:
%% - Insert a %.
%a - Abbreviated weekday name.
%A - Full weekday name
%b - Abbreviated month name.
%B - Full month name.
%d - Day of month (01 - 31).
%D - Date as %m/%d/%y.
%e - Day of month (1-31), no leading zeros.
%h - Abbreviated month name.
%H - Hour (00 - 23).
%I - Hour (00 - 12).
%j - Day number of year (001 - 366).
%m - Month number (01 - 12).
%M - Minute (00 - 59).
%n - Insert a new line.
%p - AM or PM.
%r - Time as %I:%M:%S %p.
%R - Time as %H:%M.
%S - Seconds (00 - 59).
%t - Insert a tab.
%T - Time as %H:%M:%S.
%U - Week number of year (01 - 52), Sunday is the first
     day of the week.
%w - Weekday number (Sunday = 0).
%W - Week number of year (01 - 52), Monday is the first
     day of the week.
%x - Local specific date for\&mat.
%X - Local specific time for\&mat.
%y - Year within century (00 - 99).
%Y - Year as ccyy (e.g. 1990)
%Z - Time zone name.
If for\&mat is not specified, "%a %b %d %H:%M:%S %Z %Y" is used. If GMT is specified, the time will be formatted as Greenwich Mean Time. If the argument is not specified or is empty, then the local timezone will be used as defined by the TIMEZONE environment variable.

The convertclock Command

convertclock dateString ?GMT|{}? ?baseClock?
Convert dateString to an integer clock value (see getclock). This command can parse and convert virtually any standard date and/or time string, which can include standard time zone mnemonics. If only a time is specified, the current date is assumed. If the string does not contain a time zone mnemonic, the local time zone is assumed, unless the GMT argument is specified, in which case the clock value is calculated assuming that the specified time is relative to Greenwich Mean Time. If baseClock is specified, it is taken as the current clock value. This is useful for determining the time on a specific day. The character string consists of zero or more specifications of the following form: time - A time of day, which is of the form hh[:mm[:ss]] [meridian] [zone] or hhmm [meridian] [zone]. If no meridian is specified, hh is interpreted on a 24-hour clock. date - A specific month and day with optional year. The acceptable formats are mm/dd[/yy], yyyy/mm/dd, monthname dd[, yy], dd monthname [yy], and day, dd monthname yy. The default year is the current year. If the year is less then 100, then 1900 is added to it. relative time - A specification relative to the current time. The for\&mat is number unit; acceptable units are year, fortnight, month, week, day, hour, minute (or min), and second (or sec). The unit can be specified as a singular or plural, as in 3 weeks. These modifiers may also be specified: tomorrow, yesterday, today, now, last, this, next, ago. The actual date is calculated according to the following steps. First, any absolute date and/or time is processed and converted. Using that time as the base, day-of-week specifications are added. Next, relative specifications are used. If a date or day is specified, and no absolute or relative time is given, midnight is used. Finally, a correction is applied so that the correct hour of the day is produced after allowing for daylight savings time differences. convertclock ignores case when parsing all words. The names of the months and days of the week can be abbreviated to their first three letters, with optional trailing period. Periods are ignored in any timezone or meridian values. The only dates in the range 1902 and 2037 may be converted. Some examples are:
    convertclock "14 Feb 92"
    convertclock "Feb 14, 1992 12:20 PM PST"
    convertclock "12:20 PM Feb 14, 1992"

The getclock Command

getclock