@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
str
: The string to search within.search
: The value to search for. Can be a string, number, or boolean.position
(optional): The position within the string to start the search. Default is the length of the string (search starts from the end). If a string is provided, it will be converted to a number.
Return value
- The zero-based index of the last occurrence of the specified value.
- If the value is not found, -1 is returned.
Examples
Basic usage
// Find the last occurrence of a characterconst str = "Hello world";const lastO = String.lastIndexOf(str, "o");print(lastO); // Outputs: 7 (position of 'o' in "world")
// Finding a substringconst lastL = String.lastIndexOf(str, "l");print(lastL); // Outputs: 9 (position of the last 'l' in "world")
With position parameter
// Start searching from a specific positionconst text = "hello hello hello";// Search for the last 'hello' before index 10const result = String.lastIndexOf(text, "hello", 10);print(result); // Outputs: 6 (position of the second "hello")
// Start searching from the beginningconst 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 existconst notFound = String.lastIndexOf("Hello world", "xyz");print(notFound); // Outputs: -1
// Starting search before where the value existsconst 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-sensitiveconst 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 extensionfunction 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 sentencefunction 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"