string last

Gives the index of the last match of a substring.

string last NEEDLE HAYSTACK ?LAST?

Description

Searches HAYSTACK for the text NEEDLE, from LAST back to the start, and gives the index of the first character of the last match that starts at or before LAST. The match is exact, as for string first.

LAST is end by default. A LAST past the end counts as end, and a LAST below 0 gives -1. An empty NEEDLE gives -1.

Arguments

ArgumentTypeDefaultDescription
NEEDLEstringrequiredThe text to find.
HAYSTACKstringrequiredThe text to search.
LASTindexendThe last index where a match can start.

Returns

The index of the match, or -1 when HAYSTACK has no match.

Examples

% string last ab xxabab
4

% string last ab xxabab 3
2

% string last ab xxabab 1
-1

% string last ab xxabab 100
4

Errors

% string last a
wrong # args: should be "string last needleString haystackString ?startIndex?"
-errorcode TCL WRONGARGS

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

Differences from Tcl

Facts

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

See also