Description
The indexOf
function returns the position at which the substring needle
is found within the string haystack
. If needle
is not found, the function returns -1
. The optional parameter index
can be used to specify which occurrence of needle
in haystack
should be considered.
Note: Cannot be persisted using persist
.
Syntax
indexOf(haystack, needle[, index])
Arguments
Argument | Description |
---|---|
haystack |
The string in which to search for needle . |
needle |
The substring to search for within haystack . |
index (optional) |
Specifies which occurrence of needle to return. By default, the first occurrence is considered. |
Return Types
The function returns the position of the occurrence of needle
in haystack
as an integer. If needle
is not found, the function returns -1
.
Examples
Description | Formula | Result |
---|---|---|
First occurrence of "E" |
indexOf("Dear Elephant, Unicorn or Pegasus?", "E") |
7 |
Second occurrence of "E" |
indexOf("Dear Elephant, Unicorn or Pegasus?", "E", 1) |
15 |
Substring not found |
indexOf("Hello World", "Z") |
-1 |
Notes
- Case sensitivity: The function is case-sensitive.
- Index: The
index
parameter starts at0
, which corresponds to the first occurrence.
Comments
0 comments
Please sign in to leave a comment.