Difference between revisions of "SHIP:Sail:indexOf"

From Serious Documentation
Jump to: navigation, search
Line 8: Line 8:
 
*{{SailFunc|lastIndexOf}}, {{SailFunc|subStr}}, {{SailFunc|subString}}, {{SailFunc|toString}}
 
*{{SailFunc|lastIndexOf}}, {{SailFunc|subStr}}, {{SailFunc|subString}}, {{SailFunc|toString}}
 
== Prototype ==
 
== Prototype ==
<code>{{DataType|Integer}} indexOf({{DataType|String}} original,{{DataType|String}} tofind);</code>
+
<code>{{DataType|Integer}} {{SailFunc|indexOf }}({{DataType|String}} original,{{DataType|String}} tofind);</code>
<code>{{DataType|Integer}} indexOf({{DataType|String}} original,{{DataType|String}} tofind, {{DataType|Integer}} offset);</code>
+
<code>{{DataType|Integer}} {{SailFunc|indexOf }}({{DataType|String}} original,{{DataType|String}} tofind, {{DataType|Integer}} offset);</code>
 
=== Parameters/Return Value ===
 
=== Parameters/Return Value ===
 
{| class="wikitable" style="margin: 1em 1em;"  
 
{| class="wikitable" style="margin: 1em 1em;"  

Revision as of 08:09, 13 July 2014

Function Returns Introduced Description
indexOf Integer 2-3 Returns the index of the first found occurrence (if any) of a substring within another string, optionally starting at a specified offset. If the substring is not found, -1 is returned.

See Also:

Prototype

Integer indexOf (String original,String tofind); Integer indexOf (String original,String tofind, Integer offset);

Parameters/Return Value

Parameter Data Type Description
original String String to search in
tofind String String to find in original
offset Integer index in original string 0...(length-1)
Return Integer Index of first occurrence of tofind in original, or -1 if not found

Detailed Description

The indexOf function returns the index in the original string of first occurrence of the string tofind in the original string, or -1 if the string is not found.

An optional offset may be specified, which specifies that the search in the original string begins at that specified offset. If the offset is negative, the search will begin at offset 0. If the offset is past the end of the original string, the function will always return -1.

Examples

Example Result Notes
indexOf("feb","janfebmar"); 3
indexOf("feb","janfebmarfeb"); 3 returns first found
indexOf("feb","janfebmar",4); -1 starts searching on "ebmar"
indexOf("feb","janfebmar",-1); 3 starts searching at 0
indexOf("feb","janfebmar",23); -1 past end, always returns -1