Index of: Difference between revisions

From Kolmafia
Jump to navigation Jump to search
imported>StDoodle
Created page with '{{ #vardefine:name|index_of}}{{ #vardefine:return_type|int}}{{ FunctionPage| name={{#var:name}}| function_category=String Handling Routines| function1={{Function| name={{#var:n…'
 
imported>StDoodle
mNo edit summary
Line 57: Line 57:


see_also={{SeeAlso|last_index_of}}|
see_also={{SeeAlso|last_index_of}}|
special=If {{pspan|find}} does not occur in {{pspan|search}} (or the portion used, when {{pspan|start}} is specified), this function returns -1.|
special=If {{pspan|find}} does not occur in {{pspan|search}} (or the portion searched, when {{pspan|start}} is specified), this function returns -1.|
}}
}}

Revision as of 18:21, 11 March 2010

Function Syntax

int index_of(string search ,string find )

int index_of(string search ,string find ,int start )

  • search in the string to search in
  • find is the string you're trying to find
  • start is the (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. 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.

Code Sample

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

index_of( "abcdefghijklmnopqrstuvwxyz" , "c" );

Would result in:

Returned: 2

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

index_of( "Bob, ignore me, I said to Bob one day." , "Bob" , 3);

Would result in:

Returned: 26

See Also

last_index_of()

Special

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