Skip to main content

Last Index

Finds the index (position) of the last occurrence of a substring within a string.

Common Properties

  • Name - The custom name of the node.
  • Color - The custom color of the node.
  • Delay Before (sec) - Waits in seconds before executing the node.
  • Delay After (sec) - Waits in seconds after executing node.
  • Continue On Error - Automation will continue regardless of any error. The default value is false.
info

If the ContinueOnError property is true, no error is caught when the project is executed, even if a Catch node is used.

Inputs

  • String - The string to search in.
  • Substring - The substring to find.

Options

This node does not have configurable options.

Output

  • Result - Index position of the last occurrence (0-based), or -1 if not found.

How It Works

The Last Index node searches for the last occurrence of a substring and returns its starting position:

  • 0-based indexing: First character is at index 0
  • Case-sensitive: "Hello" and "hello" are different
  • Returns -1: If substring is not found
  • Last occurrence only: Use Index for the first occurrence

Usage Examples

Example 1: Find Last Occurrence

  • String: "Hello World Hello"
  • Substring: "Hello"
  • Result: 12 (last "Hello" starts at position 12)

Example 2: Find Last Character

  • String: "Hello World"
  • Substring: "o"
  • Result: 7 (last 'o' in "World")

Example 3: Find File Extension

  • String: "archive.tar.gz"
  • Substring: "."
  • Result: 11 (last dot before "gz")

Example 4: Find Last Path Separator

  • String: "/home/user/documents/file.txt"
  • Substring: "/"
  • Result: 19 (last slash before "file.txt")

Example 5: Substring Not Found

  • String: "Hello World"
  • Substring: "xyz"
  • Result: -1

Tips

  • Index is 0-based - first character is at position 0
  • Result of -1 means substring was not found
  • Perfect for finding file extensions (last dot in filename)
  • Useful for finding last path separator
  • Search is case-sensitive - convert to same case first if needed
  • Use Index to find the first occurrence instead
  • Great for parsing paths, URLs, and hierarchical data

Common Errors and Solutions

Error: Returns -1 when substring should exist

  • Cause: Case sensitivity issue
  • Solution: Use Lowercase or Uppercase on both strings before searching

Error: Expected different result than Index node

  • Cause: Substring appears only once (both return same position)
  • Solution: This is correct behavior - if substring appears once, first and last are the same

Practical RPA Examples

Example: Extract File Name from Path

String: "/home/user/documents/report.pdf"
Substring: "/"
LastIndex: 19
FileName: Extract from index 20 onwards
Result: "report.pdf"

Example: Get File Extension

String: "document.pdf"
Substring: "."
LastIndex: 8
Extension: Extract from index 9 onwards
Result: "pdf"

Example: Handle Files with Multiple Dots

String: "backup.2024.01.15.tar.gz"
Substring: "."
LastIndex: 21
Extension: "gz" (not "2024" or other parts)

Example: Parse Email Domain

String: "user.name@company.example.com"
Substring: "."
LastIndex: 25
Top-level domain: Extract from index 26
Result: "com"

Example: Find Last Occurrence in URL

String: "https://example.com/api/v1/users"
Substring: "/"
LastIndex: 27
Last segment: Extract from index 28
Result: "users"

Example: Extract Parent Directory

String: "/var/log/application/error.log"
Substring: "/"
LastIndex: 20
Parent directory: Extract from 0 to 20
Result: "/var/log/application"

Comparison with Index Node

AspectIndexLastIndex
Search directionFirst occurrenceLast occurrence
Use caseFind first delimiterFind last delimiter
File extensionsGets first dotGets last dot (correct)
Path parsingGets first slashGets last slash (usually needed)
    • Find first occurrence
  • IndexAny - Find first occurrence of any character
  • Includes - Check if substring exists
  • Cut - Split string at separator
  • Split - Split into array