Difference between revisions of "Last index of"

From Kolmafia
Jump to navigation Jump to search
imported>Bale
m
imported>Winterbay
(Adding three parameter version)
Line 13: Line 13:
 
parameter1={{Param|string|search}}|
 
parameter1={{Param|string|search}}|
 
parameter2={{Param|string|find}}|
 
parameter2={{Param|string|find}}|
 +
}}|
 +
 +
function2={{Function|
 +
name={{#var:name}}|
 +
aggregate={{#var:aggregate}}|
 +
return_type={{#var:return_type}}|
 +
return_also={{#var:return_also}}|
 +
parameter1={{Param|string|search}}|
 +
parameter2={{Param|string|find}}|
 +
parameter3={{Param|int|end}}|
 
p1desc={{Pspan|search}} in the string to search in|
 
p1desc={{Pspan|search}} in the string to search in|
 
p2desc={{Pspan|find}} is the string you're trying to find|
 
p2desc={{Pspan|find}} is the string you're trying to find|
 +
p3desc={{Pspan|end}} is the (optional) ending position (i.e. where the string is truncated before the search is performed)|
 
}}|
 
}}|
  
Line 30: Line 41:
 
<pre>Returned: 28</pre>
 
<pre>Returned: 28</pre>
 
}}|
 
}}|
 +
 +
code2={{CodeSample|
 +
description=Returns the last instance of the phrase "la", ignoring the last three characters.|
 +
code=
 +
<syntaxhighlight>
 +
string example = "I'm not listening, la la la la";
 +
last_index_of( example , "la" , length(example) - 3 );
 +
</syntaxhighlight>|
 +
moreinfo=
 +
Would result in:
 +
<pre>
 +
Returned: 25
 +
</pre>}}|
  
 
see_also={{SeeAlso|index_of}}|
 
see_also={{SeeAlso|index_of}}|

Revision as of 21:53, 6 July 2011

Function Syntax

int last_index_of(string search ,string find )

int last_index_of(string search ,string find ,int end )

  • search in the string to search in
  • find is the string you're trying to find
  • end is the (optional) ending position (i.e. where the string is truncated before the search is performed)

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.

Code Sample

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

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.

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

index_of()

Special

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