Last index of: Difference between revisions

From Kolmafia
Jump to navigation Jump to search
Convert to Template:Function2 format
No edit summary
 
Line 9: Line 9:
|function1.param2.type=string
|function1.param2.type=string
|function1.param2.description=String you're trying to find
|function1.param2.description=String you're trying to find
|function1.param3=
|function1.param3=end
|function1.param3.type=int
|function1.param3.type=int
|function1.param3.optional=yes
|function1.param3.optional=yes

Latest revision as of 12:34, 23 December 2020

Function Syntax

int last_index_ofstring search, string find, int? end )

Returns the last index of the second string in the first, or -1 if no match is found.
  • search: String to search in
  • find: String you're trying to find
  • end: (optional) ending position. If omitted, this defaults to length(search).

This function searches through the string search, looking for the string find. This function returns the position where find last occurs in search.

This function returns a 0-indexed value, meaning that the first character of search counts as 0.

If find does not occur in search, this function returns -1.

Code Sample

Returns the last instance of the phrase "la" in the following phrase.

// Matched at:
//                                          vv
last_index_of( "I'm not listening, la la la la" , "la" );

Would result in:

Returned: 28

Returns the last instance of the phrase "la", ignoring the last three characters.

// Matched at:
//                                         vv
string example = "I'm not listening, la la la la";
last_index_of( example , "la" , length(example) - 3 );

Would result in:

Returned: 25

See Also

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