Difference between revisions of "Last index of"

From Kolmafia
Jump to navigation Jump to search
imported>StDoodle
(Created page with '{{ #vardefine:name|last_index_of}}{{ #vardefine:return_type|int}}{{ FunctionPage| name={{#var:name}}| function_category=String Handling Routines| function1={{Function| name={{#…')
 
 
(4 intermediate revisions by 4 users not shown)
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=end
 +
|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}}|
 
function_category=String Handling Routines|
 
  
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:
p1desc={{Pspan|search}} in the string to search in|
+
//                                          vv
p2desc={{Pspan|find}} is the string you're trying to find|
 
}}|
 
 
 
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=Searches for the letter "c" in the lowercase alphabet.|
 
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
+
|code2={{CodeSample
</pre>}}|
+
  |description=Returns the last instance of the phrase "la", ignoring the last three characters.
 
+
  |code=
see_also={{SeeAlso|index_of}}|
+
<syntaxhighlight lang="d">
special=If {{pspan|find}} does not occur in {{pspan|search}}, this function returns -1.|
+
// Matched at:
 +
//                                        vv
 +
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/Substring Search}}
 +
|special=
 +
}}</onlyinclude>
 +
[[Category:String Handling Routines]]

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