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.
If you don't want to distinguish between upper and lower case letters, we recommend that you unify either the specified text or the searched string with the functions
lower()
orupper()
.
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.
index("Ninox is great!", "ox")
Result: 3
index("Ninox is great!", "N")
Result: 0
index("Ninox is great!", "oX")
Result: -1 (no exact match found)
Arrays
index([myArray], match)
determines the first position of a given match in an array, starting with 0.
let myItems := [1, 2, 3, 4];
index(myItems, 3)
Result: 2
let myItems := ["A", "C", "D", "E", "F", "B"];
index(myItems, "D")
Result: 2
index(["Ninox is great!"], "ox")
Result: -1 (no exact match found)
See also
contains
, which checks if a given string or array contains the exact given match.