In Java, lastIndexOf() returns the zero-based position of the last occurrence of a character or substring in a string. It works like indexOf(), but searches from the right end towards the start. If nothing matches, it returns -1.
It is the natural tool for anything that sits at the end of text: the extension after the last dot in "report.final.pdf", the file name after the last slash in a path, or the final word of a sentence.
lastIndexOf() is an instance method of java.lang.String, available since JDK 1.0. Call it on a string, as in text.lastIndexOf(...); calling it on a null reference throws NullPointerException. It only reads the text; the string itself is never changed.
Syntax
Pass a char or a String; optionally pass the index where the backward search starts.
java
int dot = fileName.lastIndexOf('.'); // last '.'
int slash = path.lastIndexOf("/"); // last "/"
int prev = text.lastIndexOf("ab", 5); // last "ab" starting at or before 5
Call
What it does
"a/b/c".lastIndexOf('/')
Returns 3, the right-most slash.
"abcabc".lastIndexOf("abc")
Returns 3.
"abcabc".lastIndexOf("abc", 2)
Only matches starting at index 2 or earlier count: returns 0.
"abc".lastIndexOf('z')
Returns -1.
With fromIndex the search runs backwards: a match counts only if it starts at fromIndex or earlier. A fromIndex at or past the end searches the whole string; a negative one returns -1. No overload throws for a bad index.
Library declarations
java
public int lastIndexOf(int ch);
public int lastIndexOf(int ch, int fromIndex);
public int lastIndexOf(String str);
public int lastIndexOf(String str, int fromIndex);
Parameters
Parameter
Description
ch
The character (or Unicode code point) to look for.
str
The substring to look for. Must not be null.
fromIndex
Optional. The highest start index a match may have. Defaults to the end of the string.
Return Value
Return Value
Description
int
The index where the last match starts.
-1
When there is no match.
Java Examples
Example 1 — Get the Extension After the Last Dot
A file name can contain several dots. Compare the first dot with the last one.
java
public class LastIndexOfExtension {
public static void main(String[] args) {
String file = "report.final.pdf";
int firstDot = file.indexOf('.');
int lastDot = file.lastIndexOf('.');
System.out.println("first dot: " + firstDot);
System.out.println("last dot: " + lastDot);
System.out.println("extension: " + file.substring(lastDot + 1));
}
}
Output
first dot: 6
last dot: 12
extension: pdf
How It Works
Two dots, two answers.indexOf('.') stops at 6 (after "report"), but the extension is not there.
Scan from the right.lastIndexOf('.') returns 12, the dot just before "pdf".
Slice after it.substring(13) yields "pdf"; file itself still holds the full name.
Example 2 — Split a Path into Folder and File Name
Use the last '/' to separate the directory part from the file name.
java
public class LastIndexOfPath {
public static void main(String[] args) {
String path = "/home/ada/docs/notes.txt";
int slash = path.lastIndexOf('/');
String folder = path.substring(0, slash);
String name = path.substring(slash + 1);
System.out.println("slash at: " + slash);
System.out.println("folder: " + folder);
System.out.println("file: " + name);
}
}