Last index of: Difference between revisions

From Kolmafia
Jump to navigation Jump to search
imported>Winterbay
Adding three parameter version
Convert to Template:Function2 format
Line 1: Line 1:
{{
<onlyinclude>{{{{{format|Function2}}}
#vardefine:name|last_index_of}}{{
|name=last_index_of
#vardefine:return_type|int}}{{
|function1.return_type=int
|function1.description=Returns the last index of the second string in the first, or -1 if no match is found.
|function1.param1=search
|function1.param1.type=string
|function1.param1.description=String to search in
|function1.param2=find
|function1.param2.type=string
|function1.param2.description=String you're trying to find
|function1.param3=
|function1.param3.type=int
|function1.param3.optional=yes
|function1.param3.description=(optional) ending position. If omitted, this defaults to <code>length(search)</code>.
|description=<p>This function searches through the string {{pspan|search}}, looking for the string {{pspan|find}}. This function returns the position where {{pspan|find}} last occurs in {{pspan|search}}.</p>


FunctionPage|
<p>This function returns a 0-indexed value, meaning that the first character of {{pspan|search}} counts as 0.</p>
name={{#var:name}}|


function1={{Function|
<p>If {{pspan|find}} does not occur in {{pspan|search}}, this function returns -1.</p>
name={{#var:name}}|
|code1={{CodeSample
aggregate={{#var:aggregate}}|
  |title=Code Sample
return_type={{#var:return_type}}|
  |description=Returns the last instance of the phrase "la" in the following phrase.
return_also={{#var:return_also}}|
  |code=
parameter1={{Param|string|search}}|
<syntaxhighlight lang="d">
parameter2={{Param|string|find}}|
// Matched at:
}}|
//                                          vv
 
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|
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)|
}}|
 
function_description=This function searches through the string {{pspan|search}}, looking for the string {{pspan|find}}. This function returns the position where {{pspan|find}} last occurs in {{pspan|search}}. This function returns a 0-indexed value, meaning that the first character of {{pspan|search}} counts as 0.|
 
code1={{CodeSample|
title=Code Sample|
description=Returns the last instance of the phrase "la" in the following phrase.|
code=
<syntaxhighlight>
last_index_of( "I'm not listening, la la la la" , "la" );
last_index_of( "I'm not listening, la la la la" , "la" );
</syntaxhighlight>|
</syntaxhighlight>
moreinfo=
  |moreinfo=Would result in:
Would result in:
<pre>Returned: 28</pre>
<pre>Returned: 28</pre>
}}|
}}
 
|code2={{CodeSample
code2={{CodeSample|
  |description=Returns the last instance of the phrase "la", ignoring the last three characters.
description=Returns the last instance of the phrase "la", ignoring the last three characters.|
  |code=
code=
<syntaxhighlight lang="d">
<syntaxhighlight>
// Matched at:
//                                        vv
string example = "I'm not listening, la la la la";
string example = "I'm not listening, la la la la";
last_index_of( example , "la" , length(example) - 3 );
last_index_of( example , "la" , length(example) - 3 );
</syntaxhighlight>|
</syntaxhighlight>
moreinfo=
  |moreinfo=Would result in:
Would result in:
<pre>Returned: 25</pre>
<pre>
Returned: 25
</pre>}}|
 
see_also={{SeeAlso|index_of}}|
special=If {{pspan|find}} does not occur in {{pspan|search}}, this function returns -1.|
}}
}}
 
|see_also={{SeeAlso/Substring Search}}
|special=
}}</onlyinclude>
[[Category:String Handling Routines]]
[[Category:String Handling Routines]]

Revision as of 12:33, 23 December 2020

Function Syntax

int last_index_ofstring search, string find, int? )

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
  • : (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()