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
| Argument | Type | Default | Description |
|---|---|---|---|
| NEEDLE | string | required | The text to find. |
| HAYSTACK | string | required | The text to search. |
| LAST | index | end | The 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
- In Tcl 9.0.4 the message of a wrong count of arguments names the third argument
?lastIndex?(issue 662).
Facts
- Capability
- None.
- Tcl release
- Tcl 9.1 and 8.6.
- Since
- ztcl 1.0.0-beta.1