lastIndexOf (String)
  • 23 May 2023
  • Dark
    Light
  • PDF

lastIndexOf (String)

  • Dark
    Light
  • PDF

Article summary

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.

Note:

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 as sales.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 the str value.

  • The output 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.

    lastIndexOfString-01

Example 2

In this IAP example:

  • The str variable has been statically set as sales.department@itential.com.

  • The searchValue variable has been statically set as "." (a period).

  • The fromIndex variable has been set to 20 (a random number selected for demo purposes), indicating the search should start at position 20 of the str value and proceed to the left of that starting index position.

  • The output 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.

    lastIndexOfString-02

Example 3

In this IAP example:

  • The str variable has been statically set as sales.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 the str value.

  • The output index value will be -1, because there is no "Z" in the str value.

    lastIndexOfString-03

Example 4

In this IAP example:

  • The str variable has been statically set as sales.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 the str value.

  • The output index value will be 0 in reference to the first letter of the str value, which is the letter "s".

    lastIndexOfString-04


Was this article helpful?

What's Next
Changing your password will log you out immediately. Use the new password to log back in.
First name must have atleast 2 characters. Numbers and special characters are not allowed.
Last name must have atleast 1 characters. Numbers and special characters are not allowed.
Enter a valid email
Enter a valid password
Your profile has been successfully updated.