Last index of: Difference between revisions
Jump to navigation
Jump to search
imported>Winterbay Adding three parameter version |
Convert to Template:Function2 format |
||
Line 1: | Line 1: | ||
{{ | <onlyinclude>{{{{{format|Function2}}} | ||
|name=last_index_of | |||
|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> | |||
<p>This function returns a 0-indexed value, meaning that the first character of {{pspan|search}} counts as 0.</p> | |||
<p>If {{pspan|find}} does not occur in {{pspan|search}}, this function returns -1.</p> | |||
|code1={{CodeSample | |||
|title=Code Sample | |||
|description=Returns the last instance of the phrase "la" in the following phrase. | |||
|code= | |||
<syntaxhighlight lang="d"> | |||
// Matched at: | |||
// vv | |||
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/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_of( string 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