@rbxts/jsnatives



lastIndexOf

Returns the index of the last occurrence of a specified value in a string.

Signature

function lastIndexOf(str: string, search: PatternAdmissible, position?: NumericAdmissible): number

Description

The lastIndexOf function returns the position of the last occurrence of a specified value in a string. The search begins at the specified position and moves backwards toward the beginning of the string. The search is case-sensitive.

If the specified value is not found, it returns -1.

Parameters

Return value

Examples

Basic usage

// Find the last occurrence of a character
const str = "Hello world";
const lastO = String.lastIndexOf(str, "o");
print(lastO); // Outputs: 7 (position of 'o' in "world")
// Finding a substring
const lastL = String.lastIndexOf(str, "l");
print(lastL); // Outputs: 9 (position of the last 'l' in "world")

With position parameter

// Start searching from a specific position
const text = "hello hello hello";
// Search for the last 'hello' before index 10
const result = String.lastIndexOf(text, "hello", 10);
print(result); // Outputs: 6 (position of the second "hello")
// Start searching from the beginning
const fromStart = String.lastIndexOf(text, "hello", 0);
print(fromStart); // Outputs: 0 (only finds the first "hello")

Using non-string search values

// Searching for a number (converted to string)
const numText = "There are 42 apples and 42 oranges";
const lastNum = String.lastIndexOf(numText, 42);
print(lastNum); // Outputs: 24 (position of the second "42")
// Searching for a boolean (converted to string)
const boolText = "Is it true or true?";
const lastTrue = String.lastIndexOf(boolText, true);
print(lastTrue); // Outputs: 14 (position of the second "true")

Not found cases

// Searching for a value that doesn't exist
const notFound = String.lastIndexOf("Hello world", "xyz");
print(notFound); // Outputs: -1
// Starting search before where the value exists
const tooEarly = String.lastIndexOf("Hello world", "world", 5);
print(tooEarly); // Outputs: -1 (search starts at index 5, before "world" begins)

Case-sensitive search

// Searches are case-sensitive
const text = "Hello World hello";
const lastHello = String.lastIndexOf(text, "hello");
print(lastHello); // Outputs: 12 (only finds lowercase "hello")
const lastHelloUppercase = String.lastIndexOf(text, "Hello");
print(lastHelloUppercase); // Outputs: 0 (only finds "Hello" with uppercase 'H')

Practical applications

// Find the file extension
function getFileExtension(filename: string): string {
const dotIndex = String.lastIndexOf(filename, ".");
if (dotIndex === -1) {
return "";
}
return String.slice(filename, dotIndex);
}
print(getFileExtension("document.pdf")); // Outputs: ".pdf"
print(getFileExtension("image.jpg")); // Outputs: ".jpg"
print(getFileExtension("noextension")); // Outputs: ""
// Find the last word in a sentence
function getLastWord(sentence: string): string {
const lastSpaceIndex = String.lastIndexOf(sentence, " ");
if (lastSpaceIndex === -1) {
return sentence;
}
return String.slice(sentence, lastSpaceIndex + 1);
}
print(getLastWord("Hello beautiful world")); // Outputs: "world"
print(getLastWord("SingleWord")); // Outputs: "SingleWord"