- 23 May 2023
-
DarkLight
-
PDF
lastIndexOf (String)
- Updated on 23 May 2023
-
DarkLight
-
PDF
lastIndexOf (string) Task Purpose
The lastIndexOf string task allows users to identify 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). In other words, the search is backwards starting from a position identified by the fromIndex
value. This method returns a number (index
) that represents the position where the specified search value occurs.
The lastIndexOf task does not change the value of the original string.
Potential Use Case
Let's say 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 potentially use the lastIndexOf task and extract it in a subsequent slice
or substring
task.
Properties
Incoming | Type | Description |
---|---|---|
str |
String | Required. The string that contains the searchValue to be located. |
searchValue |
String | Required. The value to search for in the str . |
fromIndex |
Number | Optional. The position in the string to begin the search. Leaving this value empty will default to the end of the str and the full string is searched. When counting characters in a string, indexing begins at position '0' (the first str value). If the fromIndex value is greater than zero, the search will begin to the left of that specified index position. If '0' or a number less than '0', only the first character in the string will be considered and then search the string backwards from there. |
Outgoing | Type | Description |
---|---|---|
index |
Number | The position in the str where the searchValue was first identified. If the searchValue is not found in the specified str , the index property will return -1 . |
Example 1
In this IAP example:
-
The
str
variable has been statically set assales.department@itential.com
. -
The
searchValue
variable has been statically set as"."
(a period). -
The
fromIndex
variable has been intentionally left empty, indicating the search should start at the end of thestr
value. -
The output
index
value will be25
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 thestr
variable.
Example 2
In this IAP example:
-
The
str
variable has been statically set assales.department@itential.com
. -
The
searchValue
variable has been statically set as"."
(a period). -
The
fromIndex
variable has been set to20
(a random number selected for demo purposes), indicating the search should start at position 20 of thestr
value and proceed to the left of that starting index position. -
The output
index
value will be5
because the first instance of a period"."
, starting left from position 20, can be found at character position 5 in thestr
variable.
Example 3
In this IAP example:
-
The
str
variable has been statically set assales.department@itential.com
. -
The
searchValue
variable has been statically set as"Z"
. -
The
fromIndex
variable has been intentionally left empty, indicating the search should start at the end of thestr
value. -
The output
index
value will be-1
, because there is no"Z"
in thestr
value.
Example 4
In this IAP example:
-
The
str
variable has been statically set assales.department@itential.com
. -
The
searchValue
variable has been statically set as"s"
. -
The
fromIndex
variable has been set to-100
(a random number selected for demo purposes). Since negative numbers are considered as a '0', the search will begin at the first letter of thestr
value. -
The output
index
value will be0
in reference to the first letter of thestr
value, which is the letter"s"
.