In Java, sb.lastIndexOf(str) searches a StringBuilder from right to left and returns the starting index of the last occurrence of str, or -1 if not found.
Even though the search moves backward, the returned index still counts from 0 at the beginning of the builder. This makes it ideal for editing a file extension or swapping the final comma in a formatted list.
An instance method of the java.lang.StringBuilder class (since Java 1.5, JDK 1.5): a final, mutable sequence of UTF-16 characters that is not thread-safe. Call it on an existing builder, as in sb.lastIndexOf(...). No import is needed because java.lang is always visible.
Syntax
Search backward from the end with one argument, or from fromIndex with two.
Java
StringBuilder sb = new StringBuilder("a, b, c");
int last = sb.lastIndexOf(", "); // 4
int prev = sb.lastIndexOf(", ", 3); // 1
Call
What it does
sb.lastIndexOf(".")
Finds the rightmost dot (useful before replacing an extension).
sb.lastIndexOf(", ", cutoff)
Finds the rightmost match starting at or before cutoff.
sb.lastIndexOf("x")
Returns -1 if "x" never appears.
Like indexOf(), lastIndexOf() requires a String argument—passing a char literal will not compile.
Library declarations
Java
// java.lang.StringBuilder
public int lastIndexOf(String str)
public int lastIndexOf(String str, int fromIndex)
Parameters
Parameter
Description
str (String)
Non-null substring to locate.
fromIndex (int)
Optional rightmost starting index for the backward search.
Return Value
Return Value
Description
int
Zero-based starting index of the last match, or -1 if none exists.
Extracts the tail after the last separator as a String.
Java Examples
Example 1 — Change the Last Comma Into "and" In Place
Format a human-readable list by finding the rightmost ", " and replacing it with " and ".
Java
public class StringBuilderLastIndexOfComma {
public static void main(String[] args) {
StringBuilder list = new StringBuilder("red, green, blue");
int lastComma = list.lastIndexOf(", ");
if (lastComma != -1) {
list.replace(lastComma, lastComma + 2, " and ");
}
System.out.println(list.toString());
}
}
Output
red, green and blue
How It Works
Scan from the right.lastIndexOf(", ") skips the first comma at index 3 and returns index 10.
Replace two characters. Swapping indexes 10..12 replaces only that final comma-space pair.
Builder grows automatically. Inserting five characters in place of two yields red, green and blue.
Example 2 — Update a File Extension Using lastIndexOf(".")
Find the last dot (not an earlier folder dot) to change a filename extension safely.
Java
public class StringBuilderLastIndexOfExt {
public static void main(String[] args) {
StringBuilder file = new StringBuilder("v1.0/archive.tar.gz");
int dot = file.lastIndexOf(".");
int slash = file.lastIndexOf("/");
if (dot > slash) {
file.replace(dot, file.length(), ".zip");
}
System.out.println("Updated: " + file);
}
}
Output
Updated: v1.0/archive.tar.zip
How It Works
Rightmost dot.lastIndexOf(".") ignores v1.0 and .tar, returning index 16 (.gz).
Ensure dot is in filename. Checking dot > slash confirms the dot comes after the last folder separator.
Rewrite extension in place. Replacing from index 16 to length() changes .gz to .zip.
Example 3 — Use fromIndex Cutoffs and Handle null
Limit a backward search to an earlier part of the builder, test negative fromIndex, and catch NullPointerException.