• Menu
  • Skip to primary navigation
  • Skip to main content
  • Skip to primary sidebar

JavaBeat

Java Tutorial Blog

  • Java
    • Java 7
    • Java 8
    • Java EE
    • Servlets
  • Spring Framework
    • Spring Tutorials
    • Spring 4 Tutorials
    • Spring Boot
  • JSF Tutorials
  • Most Popular
    • Binary Search Tree Traversal
    • Spring Batch Tutorial
    • AngularJS + Spring MVC
    • Spring Data JPA Tutorial
    • Packaging and Deploying Node.js
  • About Us
  • Java
    • Java 7
    • Java 8
    • Java EE
    • Servlets
  • Spring Framework
    • Spring Tutorials
    • Spring 4 Tutorials
    • Spring Boot
  • JSF Tutorials
  • Most Popular
    • Binary Search Tree Traversal
    • Spring Batch Tutorial
    • AngularJS + Spring MVC
    • Spring Data JPA Tutorial
    • Packaging and Deploying Node.js
  • About Us

String lastIndexOf() Method in Java

December 31, 2023 //  by Anees Asghar

lastIndexOf() is one of the String class methods that retrieves the last occurrence (position) of the provided character or substring. It starts searching for the specified character or substring from the 0th index and goes until the last index. If found, it retrieves the last occurrence/index of the selected character. If the selected character or substring is not found in the targeted string, then it retrieves “-1”.

This article will demonstrate all possible use cases of the lastIndexOf() method with suitable examples.

How to Use String lastIndexOf() Method in Java?

In Java, the lastIndexOf() method accepts two parameters: A character/substring (mandatory parameter) and a starting index (optional parameter). The syntax of the lastIndexOf() method is stated below:

inputString.lastIndexOf(dataType character | substring, int index);
  • “inputString” is a target string from which we want to find the last occurrence of a character or substring.
  • “character | substring” represents a character or substring that we want to find in the inputString.
  • “int index” is an optional parameter that instructs the compiler to search for the last occurrence of the specified character/substring from the start till the specified index.

The return type of this method is integer, i.e., either it retrieves the last index of the specified character/substring or -1 (if not found).

Example 1:  Searching For a Single Character

Initialize a string to check for the last occurrence of a specific character:

String inputString = "Welcome to javabeat.net";

Declare a new integer-type variable “charFoundAt”:

int charFountAt;

Now implement the lastIndexOf() method on the inputString to check the last occurrence of desired characters:

charFountAt = inputString.lastIndexOf('e');
System.out.println("The Last Occurrence of e: " 
+charFountAt);charFountAt = inputString.lastIndexOf('a');
System.out.println("The Last Occurrence of a: " 
+charFountAt);charFountAt = inputString.lastIndexOf('j');
System.out.println("The Last Occurrence of j: " 
+charFountAt);charFountAt = inputString.lastIndexOf('J');
System.out.println("The Last Occurrence of J: " +charFountAt);

Output

From the output, we concluded the following points:

  • The lastIndexOf() method successfully retrieves the last occurrences of the specified characters.
  • The stated method retrieves “-1” for character “J” which means lastIndexOf() is a case-sensitive method.

Example 2:  Searching For a Character Based on Specified Index

In this example, we use the lastIndexOf() method with two parameters to search for the last occurrence of different characters (from start up to the specified index):

String inputString = "Welcome to javabeat.net";
int charFountAt;
charFountAt = inputString.lastIndexOf('e', 10);
System.out.println("The Last Occurrence of e: " +charFountAt);
charFountAt = inputString.lastIndexOf('a', 12);
System.out.println("The Last Occurrence of a: " +charFountAt);
charFountAt = inputString.lastIndexOf('j', 12);
System.out.println("The Last Occurrence of j: " +charFountAt);

Output 

Although the last occurrence of the character “e” is 21 in the input string. However, we specify the end index as 10, so the lastIndexOf() method returns the occurrence accordingly. Similarly, it retrieves the last occurrence of the characters “a” and “j” according to the specified index.

Example 3:  Searching For a Substring

We create an inputString and initialize it with a string:

String inputString = "Welcome to javabeat.net - java tutorials site";
int charFountAt;

Next, we use the lastIndexOf() method to check the last occurrences of two substrings: “java” and “programming”:

charFountAt = inputString.lastIndexOf("java");
System.out.println("The Last Occurrence of java: " +charFountAt);
charFountAt = inputString.lastIndexOf("programming");
System.out.println("The Last Occurrence of programming: " +charFountAt);

Output

The last occurrence of the substring “java” is 26 while the substring “programming” doesn’t appear in the input string.

Example 4: Searching For a Substring Based on Specified Index

To find a substring within the limited range use the lastIndexOf() method with two parameters as follows:

String inputString = "Welcome to javabeat.net - java tutorials site";
int charFountAt;
charFountAt = inputString.lastIndexOf("java", 20);
System.out.println("The Last Occurrence of java: " +charFountAt);
charFountAt = inputString.lastIndexOf("site", 25);
System.out.println("The Last Occurrence of programming: " +charFountAt);

Output

The substring “site” exists in the inputString, but it doesn’t fall in the specified index range, i.e., 25, so the lastIndexOf() method retrieves “-1”.

Bonus Tip 1: Get the First Occurrence of a Character of Substring

Use the indexOf() method of the string class to retrieve the first occurrence of the desired character substring (instead of the last occurrence):

String inputString = "Welcome to javabeat.net - java tutorials site";
int charFountAt;
charFountAt = inputString.indexOf('e');
System.out.println("The first Occurrence of e: " +charFountAt);
charFountAt = inputString.indexOf("java");
System.out.println("The first Occurrence of java: " +charFountAt);
charFountAt = inputString.indexOf("java", 15);
System.out.println("The first Occurrence of java After 15th index: " +charFountAt);
charFountAt = inputString.indexOf("website");
System.out.println("The first Occurrence of website: " +charFountAt);

Output

That’s all about using the lastIndexOf() method in Java.

Conclusion

In Java, the lastIndexOf() of the String class retrieves the last occurrence (position) of the provided character or substring. It retrieves an integer that represents the last occurrence/index of the selected character or substring. If the selected character or substring is not found in the targeted string, then it retrieves “-1”. The stated method can accept an optional parameter “index” that instructs the compiler to search for the last occurrence of the specified character/substring within the provided index range. This article has demonstrated the working of the String lastIndexOf() method along with practical examples.

Category: Java

About Anees Asghar

Anees, a go-to expert of various technologies like PostgreSQL, Java, JS, and Linux. He has been contributing to the community through his words. A passion for serving the people excites him to craft primo content.

Previous Post: « How to Split a String by Whitespaces in Java?
Next Post: How to Check if a String is Numeric in Java »

Primary Sidebar

Follow Us

  • Facebook
  • Pinterest

FEATURED TUTORIALS

How to Initialize an Array in Java

Introduction to Java Server Faces (JSF)

Introduction to Java 6.0 New Features, Part–1

Java 6.0 Features Part – 2 : Pluggable Annotation Processing API

Introduction to Java Server Faces(JSF) HTML Tags

JavaBeat

Copyright © by JavaBeat · All rights reserved
Privacy Policy | Contact