index
To return the start position of the first match in a string or an array
With this function, you can determine the position of a searched value within a given string or an array.
Unlike contains()
, this function not only checks whether the searched value exists but also determines the numeric value of its start position within a string or an array. Counting starts at 0.
If there is no match for the searched value, the result is -1.
Strings
If the searched value is part of a string, the position of the match's first characters is returned. The result is then 0.
Arrays
If there is no array item that is identical to the searched value, the result is -1.
Syntax
index(string, string)
index([any], any)
Return
number
Examples
Strings
index(string, match)
determines the first position of a given match in a string, starting with 0.
Result: 3
Result: 0
Result: -1 (no exact match found)
Arrays
index([myArray], match)
determines the first position of a given match in an array, starting with 0.
Result: 2
Result: 2
Result: -1 (no exact match found)
See also
contains
, which checks if a given string or array contains the exact given match.
Last updated