Back to Home
$getTextSplitIndex
$getTextSplitIndex searches the current text split for a
specific value and returns its index. If the value is not found, it
returns -1.
Syntax
$getTextSplitIndex[Value]
-
Value: the string to search for in the split. Can be empty.
What happens
-
$textSplitcreates the split and stores the elements. $getTextSplitIndexscans each element in order.-
If a match is found, it returns that element's index (starting from
1). - If no match is found, it returns
-1.
Example 1: Find a known value
$nomention
$textSplit[hello_world_!;_]
$getTextSplitIndex[world]
What happens:
-
$textSplitsplits the string intohello(1),world(2),!(3). -
$getTextSplitIndex[world]findsworldat index2and returns it.
Output:
2
Example 2: Search for user input
$nomention
$textSplit[hello_world_!;_]
$getTextSplitIndex[$message]
What happens:
-
$textSplitsplits the string into three elements. -
$getTextSplitIndexsearches for whatever the user typed.
If the user runs !example world:
2
If the user runs !example bdfd:
-1
Common uses
- Checking if a value exists in a split before reading or editing it
-
Finding a position to use with
$editSplitTextor$removeSplitTextElement - Validating user input against a list of allowed values
See also
- $textSplit: must be called first to create the split
- $splitText: to get an element by index
- $editSplitText: to replace an element by index
- $removeSplitTextElement: to remove an element by index