Index of: Difference between revisions

From Kolmafia
Jump to navigation Jump to search
imported>StDoodle
mNo edit summary
Convert to Template:Function2 format
 
(4 intermediate revisions by 3 users not shown)
Line 1: Line 1:
{{
<onlyinclude>{{{{{format|Function2}}}
#vardefine:name|index_of}}{{
|name=index_of
#vardefine:return_type|int}}{{
|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>


FunctionPage|
<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>
name={{#var:name}}|
function_category=String Handling Routines|


function1={{Function|
<p>If {{pspan|find}} does not occur in {{pspan|search}} (or the portion searched, when {{pspan|start}} is specified), this function returns -1.</p>
name={{#var:name}}|
|code1={{CodeSample
aggregate={{#var:aggregate}}|
  |title=Code Sample
return_type={{#var:return_type}}|
  |description=Searches for the letter "c" in the lowercase alphabet.
return_also={{#var:return_also}}|
  |code=
parameter1={{Param|string|search}}|
<syntaxhighlight lang="d">
parameter2={{Param|string|find}}|
// Matched here:
}}|
//          v
 
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|start}}|
p1desc={{Pspan|search}} in the string to search in|
p2desc={{Pspan|find}} is the string you're trying to find|
p3desc={{Pspan|start}} is the (optional) starting position|
}}|
 
function_description=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}}. 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.|
 
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 the "Bob" in a line of text, ignoring the first 3 characters.|
  |description=Searches for "Bob" in a line of text, ignoring the first 3 characters.
code=
  |code=
<syntaxhighlight>
<syntaxhighlight lang="d">
index_of( "Bob, ignore me, I said to Bob one day." , "Bob" , 3);
// Matched here:
</syntaxhighlight>|
//                                  vvv
moreinfo=
index_of( "Bob, ignore me, I said to Bob one day." , "Bob" , 3 );
Would result in:
</syntaxhighlight>
  |moreinfo=Would result in:
<pre>
<pre>
Returned: 26
Returned: 26
</pre>}}|
</pre>
}}
|see_also={{SeeAlso/Substring Search}}
|special=
}}</onlyinclude>


see_also={{SeeAlso|last_index_of}}|
[[Category:String Handling Routines]]
special=If {{pspan|find}} does not occur in {{pspan|search}} (or the portion searched, when {{pspan|start}} is specified), this function returns -1.|
}}

Latest revision as of 12:26, 23 December 2020

Function Syntax

int index_ofstring 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

See Also

contains_text() | ends_with() | index_of() | last_index_of() | starts_with()