The lastIndexOf string task identifies the last occurrence of a specific character or group of characters within a given string. It is similar to the indexOf task, with the exception that it begins searching from the end of the input string (rather than the beginning). The search proceeds backwards starting from a position identified by the fromIndex value. This method returns a number (index) representing the position where the specified search value occurs.
The lastIndexOf task does not change the value of the original string.
Suppose you have a string that contains a date within it, and you need to locate the last slash / in the date value so that you can isolate the position of the “year” value. You could use the lastIndexOf task and extract it in a subsequent slice or substring task.
In this example:
str variable has been statically set as sales.department@itential.com.searchValue variable has been statically set as "." (a period).fromIndex variable has been intentionally left empty, indicating the search should start at the end of the str value.index value will be 25 because the first instance of a period ".", starting from the end of the string, can be found at character position 25 (counting from the left) in the str variable.
In this example:
str variable has been statically set as sales.department@itential.com.searchValue variable has been statically set as "." (a period).fromIndex variable has been set to 20, indicating the search should start at position 20 of the str value and proceed to the left of that starting index position.index value will be 5 because the first instance of a period ".", starting left from position 20, can be found at character position 5 in the str variable.
In this example:
str variable has been statically set as sales.department@itential.com.searchValue variable has been statically set as "Z".fromIndex variable has been intentionally left empty, indicating the search should start at the end of the str value.index value will be -1, because there is no "Z" in the str value.
In this example:
str variable has been statically set as sales.department@itential.com.searchValue variable has been statically set as "s".fromIndex variable has been set to -100. Since negative numbers are treated as 0, the search will begin at the first letter of the str value.index value will be 0 in reference to the first letter of the str value, which is the letter "s".