Index of: Difference between revisions
imported>StDoodle mNo edit summary |
Convert to Template:Function2 format |
||
(3 intermediate revisions by 3 users not shown) | |||
Line 1: | Line 1: | ||
{{ | <onlyinclude>{{{{{format|Function2}}} | ||
|name=index_of | |||
|function1.return_type=int | |||
|function1.description=Returns the position of the second string in the first (or -1 if not found), optionally starting its search from a given position. | |||
|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=start | |||
|function1.param3.type=int | |||
|function1.param3.optional=yes | |||
|function1.param3.default=0 | |||
|function1.param3.description=(optional) starting position | |||
|description=<p>This function searches through the string {{pspan|search}}, looking for the string {{pspan|find}}. If the optional parameter {{pspan|start}} is specified, it will start searching from that position inside of {{pspan|search}}, ignoring everything that comes before {{pspan|start}} in {{pspan|search}}. This function returns the position where {{pspan|find}} first occurs in {{pspan|search}}, or -1 if {{pspan|find}} was not found.</p> | |||
<p>This function returns a 0-indexed value, meaning that the first character of {{pspan|search}} counts as 0. Note that the return value is still based on the full string {{pspan|search}} if {{pspan|start}} is specified; it simply ignores any matches made before that point.</p> | |||
<p>If {{pspan|find}} does not occur in {{pspan|search}} (or the portion searched, when {{pspan|start}} is specified), this function returns -1.</p> | |||
|code1={{CodeSample | |||
|title=Code Sample | |||
|description=Searches for the letter "c" in the lowercase alphabet. | |||
|code= | |||
<syntaxhighlight lang="d"> | |||
// Matched here: | |||
// v | |||
code1={{CodeSample| | |||
title=Code Sample| | |||
description=Searches for the letter "c" in the lowercase alphabet.| | |||
code= | |||
<syntaxhighlight> | |||
index_of( "abcdefghijklmnopqrstuvwxyz" , "c" ); | index_of( "abcdefghijklmnopqrstuvwxyz" , "c" ); | ||
</syntaxhighlight>| | </syntaxhighlight> | ||
moreinfo= | |moreinfo=Would result in: | ||
Would result in: | |||
<pre> | <pre> | ||
Returned: 2 | Returned: 2 | ||
</pre>}}| | </pre> | ||
}} | |||
code2={{CodeSample| | |code2={{CodeSample | ||
description=Searches for | |description=Searches for "Bob" in a line of text, ignoring the first 3 characters. | ||
code= | |code= | ||
<syntaxhighlight> | <syntaxhighlight lang="d"> | ||
// Matched here: | |||
// vvv | |||
index_of( "Bob, ignore me, I said to Bob one day." , "Bob" , 3 ); | index_of( "Bob, ignore me, I said to Bob one day." , "Bob" , 3 ); | ||
</syntaxhighlight>| | </syntaxhighlight> | ||
moreinfo= | |moreinfo=Would result in: | ||
Would result in: | |||
<pre> | <pre> | ||
Returned: 26 | Returned: 26 | ||
</pre>}}| | </pre> | ||
}} | |||
|see_also={{SeeAlso/Substring Search}} | |||
|special= | |||
}}</onlyinclude> | |||
[[Category:String Handling Routines]] | |||
Latest revision as of 12:26, 23 December 2020
Function Syntax
int index_of( string search, string find, int? start = 0 )
- Returns the position of the second string in the first (or -1 if not found), optionally starting its search from a given position.
- search: String to search in
- find: String you're trying to find
- start: (optional) starting position
This function searches through the string search, looking for the string find. If the optional parameter start is specified, it will start searching from that position inside of search, ignoring everything that comes before start in search. This function returns the position where find first occurs in search, or -1 if find was not found.
This function returns a 0-indexed value, meaning that the first character of search counts as 0. Note that the return value is still based on the full string search if start is specified; it simply ignores any matches made before that point.
If find does not occur in search (or the portion searched, when start is specified), this function returns -1.
Code Sample
Searches for the letter "c" in the lowercase alphabet.
// Matched here:
// v
index_of( "abcdefghijklmnopqrstuvwxyz" , "c" );
Would result in:
Returned: 2
Searches for "Bob" in a line of text, ignoring the first 3 characters.
// Matched here:
// vvv
index_of( "Bob, ignore me, I said to Bob one day." , "Bob" , 3 );
Would result in:
Returned: 26