string index

Gives the character at an index of a string.

string index STRING INDEX
string index -grapheme STRING INDEX

Description

Gives the character of STRING at INDEX. An index before the first character or after the last one gives an empty string, and so does each index of an empty string.

With -grapheme, INDEX counts grapheme clusters, and the result is the whole cluster at that place.

Arguments

ArgumentTypeDefaultDescription
STRINGstringrequiredThe text.
INDEXindexrequiredAn integer from 0, end, end-N, or M+N.

Options

OptionTypeDefaultSummary
-graphemeflagnoneCounts INDEX in grapheme clusters, and gives a whole cluster.

-grapheme

Counts INDEX in grapheme clusters, and gives a whole cluster.

INDEX counts grapheme clusters, and the result is the cluster at that place with each of its code points. An index past the last cluster gives an empty string.

With two arguments, -grapheme is the string and the other argument is the index.

% string index -grapheme "aéb" 1
é

% string index -grapheme "aéb" end
b
# With two arguments, -grapheme is the string and abc is the index.
% string index -grapheme abc
bad index "abc": must be integer?[+-]integer? or end?[+-]integer?
-errorcode NONE

Returns

One character, one grapheme cluster with -grapheme, or an empty string.

Examples

% string index abc 1
b

% string index abc end-1
b

% string index abc 1+1
c

% string index abc 5

% string index abc -1

Errors

% string index abc foo
bad index "foo": must be integer?[+-]integer? or end?[+-]integer?
-errorcode NONE

% string index abc
wrong # args: should be "string index string charIndex"
-errorcode TCL WRONGARGS

Facts

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

See also