string range

Gives the characters of a string from one index to another.

string range STRING FIRST LAST
string range -grapheme STRING FIRST LAST

Description

Gives the characters of STRING from FIRST to LAST, both included. A FIRST below 0 counts as 0, and a LAST past the end counts as the last character. When FIRST is after LAST, after the clamp, the result is an empty string.

With -grapheme, FIRST and LAST count grapheme clusters.

Arguments

ArgumentTypeDefaultDescription
STRINGstringrequiredThe text.
FIRSTindexrequiredThe index of the first character of the result.
LASTindexrequiredThe index of the last character of the result.

Options

OptionTypeDefaultSummary
-graphemeflagnoneCounts FIRST and LAST in grapheme clusters.

-grapheme

Counts FIRST and LAST in grapheme clusters.

FIRST and LAST count grapheme clusters, so the result never splits a letter from its combining marks or a flag in two. The rules of the clamp are the same as for characters.

% string range -grapheme "aéio" 1 2
éi

# With no option, the range is two code points: e and the accent.
% string range "aéio" 1 2
é
% string range -grapheme abc 0 x
bad index "x": must be integer?[+-]integer? or end?[+-]integer?
-errorcode NONE

Returns

A string, empty when the range has no character.

Examples

% string range abcdef 1 3
bcd

% string range abcdef end-2 end
def

% string range abcdef -5 2
abc

% string range abcdef 3 1

Errors

% string range abc 1
wrong # args: should be "string range string first last"
-errorcode TCL WRONGARGS

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

Facts

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

See also