Updated 2012-10-30 15:33:02 by dkf
string first string1 string2 ?startIndex?

Search string2 (the haystack) for a sequence of characters that exactly match the characters in string1 (the needle). If found, return the index of the first character in the first such match within string2. If not found, return “-1”. If startIndex is specified (in any of the forms accepted by string index), then the search is constrained to start with the character in string2 specified by the index. For example,
string first a 0a23456789abcdef 5

will return “10”, but
string first a 0123456789abcdef 11

will return “-1”.

Example:
string first a 0a23456789abcdef

returns “1” (notice this is again a zero based value).
string first "a" "0a23456789abcdef" 5

returns “10”.

This example uses the startIndex to indicate that the searching should begin at the fifth character (a 4 in the example). However the result is still a zero based index from the beginning of the string!
string first a 0123456789abcdef 11

returns a “-1”, the standard return if the string being sought is not found. An error is generated if the index is not an integer or end?-integer?.

A wish for Tcl 8.5+ or 9.0

Somehow let string first be flagged -nocase. Perhaps allowing the flag at the end where it can't be confused with and can co-exist with the starting index. Alternatively: allow string match to return the index at which the match occurred. -- Roy Terry, 7 January, 2004
-- RS same day: Isn't that duplicating what we have in
regexp -nocase -start 11 -inline a 0123456789abcdef 

Sure it is and the desire is for efficiency and also the consistency of having more string commands respect no case. There is already huge overlap between what you can do with string and regexp. In fact, you could surely write all the string commands in pure Tcl using regexp, regsub. - Roy

DKF: In performance terms, we try to go in the other direction, rewriting regexp to use string match and regsub to use string map (well, where possible).


See also edit