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

SubcommandSummary
string lengthGives the count of characters of a string.
string bytelengthGives the count of bytes of the UTF-8 form of a string. Tcl 8.6 only
string insertGives a string with text put before the character at an index. Tcl 9.1 only
string indexGives the character at an index of a string.
string rangeGives the characters of a string from one index to another.
string firstGives the index of the first match of a substring.
string lastGives the index of the last match of a substring.
string compareCompares two strings character by character.
string equalTests whether two strings are the same text.
string matchTests whether a glob pattern matches the whole of a string.
string mapReplaces each key of a list of keys and values with its value.
string repeatGives a string repeated a number of times.
string replaceGives a string with a range of characters replaced by other text.
string reverseGives the characters of a string in the reverse order.
string tolowerGives a string with its letters in lowercase.
string toupperGives a string with its letters in uppercase.
string totitleGives a string with its first character in title case and the rest in lowercase.
string trimGives a string with given characters removed at both ends.
string trimleftGives a string with given characters removed at the start.
string trimrightGives a string with given characters removed at the end.
string isTests whether a string is of a class, such as integer or alpha.
string catJoins strings with no separator.
string wordstartGives the index of the first character of the word at an index.
string wordendGives 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

Differences from Tcl

Facts

Capability
None.
Tcl release
Tcl 9.1 and 8.6.
Since
ztcl 1.0.0-beta.1

See also