string
The commands for text: length, search, compare, change, and test.
string SUBCOMMAND ?ARG ...?
Description
The commands for text. The first argument names the subcommand, and a unique prefix of its name is enough: string len abc is string length abc.
A string is a sequence of characters, and each character is one Unicode code point. An index counts characters from 0. Each argument that is an index can be an integer, end, or end-N and end+N for a count from the last character. An index can also be M+N or M-N with two integers, such as 1+1. An index before the first character or after the last one is valid: each subcommand names what it does with it.
A string can have any characters, also the NUL character. An empty string is valid for each argument that is a string.
A unique prefix of an option of compare, equal, match, map, and is is the option: -n is -nocase.
The option -grapheme of length, index, range, and reverse counts grapheme clusters in place of characters. A grapheme cluster is what a reader sees as one character, such as e with a combining accent, or a flag of two regional indicators. It needs the module ztcl::text in the build, with no package require.
Subcommands
| Subcommand | Summary |
|---|---|
string length | Gives the count of characters of a string. |
string bytelength | Gives the count of bytes of the UTF-8 form of a string. Tcl 8.6 only |
string insert | Gives a string with text put before the character at an index. Tcl 9.1 only |
string index | Gives the character at an index of a string. |
string range | Gives the characters of a string from one index to another. |
string first | Gives the index of the first match of a substring. |
string last | Gives the index of the last match of a substring. |
string compare | Compares two strings character by character. |
string equal | Tests whether two strings are the same text. |
string match | Tests whether a glob pattern matches the whole of a string. |
string map | Replaces each key of a list of keys and values with its value. |
string repeat | Gives a string repeated a number of times. |
string replace | Gives a string with a range of characters replaced by other text. |
string reverse | Gives the characters of a string in the reverse order. |
string tolower | Gives a string with its letters in lowercase. |
string toupper | Gives a string with its letters in uppercase. |
string totitle | Gives a string with its first character in title case and the rest in lowercase. |
string trim | Gives a string with given characters removed at both ends. |
string trimleft | Gives a string with given characters removed at the start. |
string trimright | Gives a string with given characters removed at the end. |
string is | Tests whether a string is of a class, such as integer or alpha. |
string cat | Joins strings with no separator. |
string wordstart | Gives the index of the first character of the word at an index. |
string wordend | Gives the index after the last character of the word at an index. |
Returns
The result of the subcommand.
Examples
% string length "buy milk"
8
% string range "buy milk" 4 end
milk
# A flag of two regional indicators, a space, and e with a combining accent: three grapheme clusters.
% string length -grapheme "\U1F1FA\U1F1F8 é"
3
% string equal [string reverse -grapheme "aé"] "éa"
1
% string map {a 1 b 2} abcab
12c12
% string is integer -strict ""
0
% string first k "buy milk"
7
# A unique prefix of a subcommand is the subcommand.
% string len abc
3
Errors
# Tcl 9.1 only
% string nope x
unknown or ambiguous subcommand "nope": must be cat, compare, equal, first, index, insert, is, last, length, map, match, range, repeat, replace, reverse, tolower, totitle, toupper, trim, trimleft, trimright, wordend, or wordstart
-errorcode NONE
# Tcl 8.6 only
% string nope x
unknown or ambiguous subcommand "nope": must be bytelength, cat, compare, equal, first, index, is, last, length, map, match, range, repeat, replace, reverse, tolower, totitle, toupper, trim, trimleft, trimright, wordend, or wordstart
-errorcode NONE
# Tcl 9.1 only
# The prefix t matches more than one subcommand.
% string t abc
unknown or ambiguous subcommand "t": must be cat, compare, equal, first, index, insert, is, last, length, map, match, range, repeat, replace, reverse, tolower, totitle, toupper, trim, trimleft, trimright, wordend, or wordstart
-errorcode NONE
Tcl 8.6 and Tcl 9.1
- Tcl 9.1 adds
string insertand the classdictofstring is, and has nostring bytelength. - In Tcl 9.1 an integer of
string is integercan have_between its digits, such as1_000.
Differences from Tcl
- Many errors have
-errorcode NONEin ztcl, where Tcl 8.6.16 and 9.0.4 give a code such asTCL VALUE INDEXorTCL LOOKUP SUBCOMMAND, and an error of a bad option has the code with no bad word at its end (issue 658).
Facts
- Capability
- None.
- Tcl release
- Tcl 9.1 and 8.6.
- Since
- ztcl 1.0.0-beta.1