string first

Gives the index of the first match of a substring.

string first NEEDLE HAYSTACK ?START?

Description

Searches HAYSTACK for the text NEEDLE, from START to the end, and gives the index of the first character of the first match. The match is exact: the case of letters counts, and NEEDLE has no pattern characters.

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

Arguments

ArgumentTypeDefaultDescription
NEEDLEstringrequiredThe text to find.
HAYSTACKstringrequiredThe text to search.
STARTindex0The index where the search starts.

Returns

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

Examples

% string first k "buy milk"
7

% string first ab xxabab 3
4

% string first ab xxabab end-1
4

% string first k "buy milk" 8
-1

% string first "" abc
-1

Errors

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

% string first k kk x
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