index_of

From Kolmafia
Jump to navigation Jump to search

Function Syntax

int index_ofstring search, string find, int? start = 0 )

Returns the position of the second string in the first (or -1 if not found), optionally starting its search from a given position.
  • search: String to search in
  • find: String you're trying to find
  • start: (optional) starting position

This function searches through the string search, looking for the string find. If the optional parameter start is specified, it will start searching from that position inside of search, ignoring everything that comes before start in search. This function returns the position where find first occurs in search, or -1 if find was not found.

This function returns a 0-indexed value, meaning that the first character of search counts as 0. Note that the return value is still based on the full string search if start is specified; it simply ignores any matches made before that point.

If find does not occur in search (or the portion searched, when start is specified), this function returns -1.

Code Sample

Searches for the letter "c" in the lowercase alphabet.

// Matched here:
//           v
index_of( "abcdefghijklmnopqrstuvwxyz" , "c" );

Would result in:

Returned: 2

Searches for "Bob" in a line of text, ignoring the first 3 characters.

// Matched here:
//                                   vvv
index_of( "Bob, ignore me, I said to Bob one day." , "Bob" , 3 );

Would result in:

Returned: 26

See Also

contains_text() | ends_with() | index_of() | last_index_of() | starts_with()