Aug 28, 2026 — Digital Edition
Bradley Ideas
Independent Journalism & Insight
Feature

String LastIndexOf in Java: A Comprehensive Guide

The String LastIndexOf() method in .NET is a powerful tool for manipulating strings in your applications. Often, you need to find the last occurrence of a particular substring within a larger string. This method allows you to accomplish just that, offering a quick and efficient way to locate characters or substrings.

C# LastIndexOf()
C# LastIndexOf()

Before delving into the intricacies of LastIndexOf(), let's briefly revisit how strings are handled in .NET. The String class is a sealed, immutable class, meaning its instances cannot be changed after they're created. Methods like LastIndexOf() are therefore essential for performing operations on strings without modifying their original values.

the logo for ultimate stringing
the logo for ultimate stringing

Understanding LastIndexOf()'s Functionality

The LastIndexOf() method returns the zero-based index of the last occurrence of the specified value within the current string. If the value is not found, it returns -1. The index where the value is found is calculated from right to left in the string.

String
String

LastIndexOf() does not consider the length of the input string or the target value. It will start searching from the last character and stop as soon as it finds the target value or exhausts the string length.

Basic Syntax

Java String: Exploring Substring Search in Java Strings with lastIndexOf(String str) | Java Tutorial
Java String: Exploring Substring Search in Java Strings with lastIndexOf(String str) | Java Tutorial

The basic syntax for using LastIndexOf() is as follows:

int lastIndex = str.LastIndexOf(value);

Here, str is the source string, and value is the substring to search for.

Case-Sensitivity and Null Checks

24 Essential JS String Methods Every Developer Should Know
24 Essential JS String Methods Every Developer Should Know

By default, LastIndexOf() is case-sensitive. To perform a case-insensitive search, you can use the StringComparison.OrdinalIgnoreCase enumerator:

int lastIndex = str.LastIndexOf(value, StringComparison.OrdinalIgnoreCase);

Before calling LastIndexOf(), ensure both the source string and the value are not null to avoid ArgumentNullException. If null values are expected, use null checks:

if (str != null && value != null) { int lastIndex = str.LastIndexOf(value); }

Super String
Super String

Use Cases and Common Scenarios

Knowing the index of the last occurrence of a substring can be crucial in various scenarios. Here are a couple:

an abstract black and white photo with lines in the center, looking up into the sky
an abstract black and white photo with lines in the center, looking up into the sky
JavaScript String Methods Cheat Sheet – Search & Find (Part 1)
JavaScript String Methods Cheat Sheet – Search & Find (Part 1)
entrelacement
entrelacement
some orange string lights are on a black background with white balls in the middle and one is glowing
some orange string lights are on a black background with white balls in the middle and one is glowing
the poster shows different types of programming
the poster shows different types of programming
String Thing 2
String Thing 2
a man holding up two fingers and string in front of his face
a man holding up two fingers and string in front of his face
a long red line on the side of a black wall
a long red line on the side of a black wall
an image of hands with different lines in the middle and one line on the top
an image of hands with different lines in the middle and one line on the top

Processing Input Data

You might use LastIndexOf() to process user input or log data. For instance, extracting a user's email address from a string containing their name, email, and other details. If the email format is consistent, LastIndexOf() can help find the starting point of the email address:

string text = "John Doe ";
int lastIndex = text.LastIndexOf('<');
string email = text.Substring(lastIndex + 1, text.Length - lastIndex - 2);

Generating Custom IDs

You can use LastIndexOf() to generate unique IDs based on some rules. For instance, creating a filename with an incrementing number if the original filename already exists:

string filename = "myfile.txt";
if (File.Exists(filename))
{
    int lastIndex = filename.LastIndexOf('.');
    string extension = filename.Substring(lastIndex);
    filename = filename.Insert(lastIndex, "(1)") + extension;
    while (File.Exists(filename))
    {
        int number = int.Parse(filename.Substring(lastIndex - 2, 1)) + 1;
        filename = filename.Substring(0, lastIndex - 2) + number + extension;
    }
}

LastIndexOf() is a versatile method that can streamline string manipulation in your applications, helping you extract, process, and generate data more efficiently.