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.