Java String lastIndexOf() Method

📘 Premium Read: Access my best content on Medium member-only articles — deep dives into Java, Spring Boot, Microservices, backend architecture, interview preparation, career advice, and industry-standard best practices.

🎓 Top 15 Udemy Courses (80-90% Discount): My Udemy Courses - Ramesh Fadatare — All my Udemy courses are real-time and project oriented courses.

▶️ Subscribe to My YouTube Channel (176K+ subscribers): Java Guides on YouTube

▶️ For AI, ChatGPT, Web, Tech, and Generative AI, subscribe to another channel: Ramesh Fadatare on YouTube

The String.lastIndexOf() method in Java is used to find the index of the last occurrence of a specified character or substring within a string. This guide will cover the method's usage, explain how it works, and provide examples to demonstrate its functionality.

Table of Contents

  1. Introduction
  2. lastIndexOf Method Syntax
  3. Examples
    • Finding the Last Index of a Character
    • Finding the Last Index of a Substring
    • Starting Search from a Specific Index
    • Handling Not Found Cases
  4. Conclusion

Introduction

The String.lastIndexOf() method is a member of the String class in Java. It allows you to locate the position of the last occurrence of a character or substring within a string. This method is particularly useful for searching, parsing, and manipulating strings based on specific characters or patterns.

lastIndexOf Method Syntax

The lastIndexOf method has several overloads:

  1. Finding the last index of a character:
public int lastIndexOf(int ch) 
  1. Finding the last index of a character starting from a specified index:
public int lastIndexOf(int ch, int fromIndex) 
  1. Finding the last index of a substring:
public int lastIndexOf(String str) 
  1. Finding the last index of a substring starting from a specified index:
public int lastIndexOf(String str, int fromIndex) 

Examples

Finding the Last Index of a Character

The lastIndexOf method can be used to find the last occurrence of a specified character within a string.

Example

public class LastIndexOfExample { public static void main(String[] args) { String message = "Hello, World!"; int lastIndexOfO = message.lastIndexOf('o'); int lastIndexOfL = message.lastIndexOf('l'); System.out.println("Last index of 'o': " + lastIndexOfO); System.out.println("Last index of 'l': " + lastIndexOfL); } } 

Output:

Last index of 'o': 8 Last index of 'l': 10 

Finding the Last Index of a Substring

The lastIndexOf method can be used to find the last occurrence of a specified substring within a string.

Example

public class LastIndexOfExample { public static void main(String[] args) { String message = "Hello, World! Hello, Java!"; int lastIndexOfHello = message.lastIndexOf("Hello"); int lastIndexOfJava = message.lastIndexOf("Java"); System.out.println("Last index of 'Hello': " + lastIndexOfHello); System.out.println("Last index of 'Java': " + lastIndexOfJava); } } 

Output:

Last index of 'Hello': 14 Last index of 'Java': 21 

Starting Search from a Specific Index

The lastIndexOf method can be used to start the search from a specified index.

Example

public class LastIndexOfExample { public static void main(String[] args) { String message = "Hello, World! Hello, Java!"; int lastIndexOfOFromIndex = message.lastIndexOf('o', 10); System.out.println("Last index of 'o' from index 10: " + lastIndexOfOFromIndex); } } 

Output:

Last index of 'o' from index 10: 8 

Handling Not Found Cases

The lastIndexOf method returns -1 if the specified character or substring is not found.

Example

public class LastIndexOfExample { public static void main(String[] args) { String message = "Hello, World!"; int lastIndexOfJava = message.lastIndexOf("Java"); System.out.println("Last index of 'Java': " + lastIndexOfJava); } } 

Output:

Last index of 'Java': -1 

Conclusion

The String.lastIndexOf() method in Java is for locating the position of the last occurrence of characters and substrings within a string. By understanding how to use this method, you can efficiently search, parse, and manipulate strings in your Java applications. Whether you are finding the last index of a character, a substring, starting the search from a specific index, or handling cases where the character or substring is not found, the lastIndexOf method provides a reliable solution for these tasks.

Comments

Spring Boot 3 Paid Course Published for Free
on my Java Guides YouTube Channel

Subscribe to my YouTube Channel (165K+ subscribers):
Java Guides Channel

Top 10 My Udemy Courses with Huge Discount:
Udemy Courses - Ramesh Fadatare