String lastIndexOf() Method in Java with Examples
The String lastIndexOf() method returns the position of the last occurrence of a given character or substring in a String.
The Index starts from 0, and if the given substring is not found it returns -1.
Example:
Java
public class L_index1 {
public static void main(String args[]) {
String Str = new String( "Hello World" );
System.out.print( "Found Last Index of l at : " );
System.out.println(Str.lastIndexOf( 'l' ));
}
}
|
Output
Found Last Index of l at : 9
Syntax
There are four variants of the lastIndexOf() method.
Method |
Description |
int lastIndexOf(char ch) |
It returns the index of the last occurrence of the specified character. |
public int lastIndexOf(char ch, int fromIndex) |
It returns the index of the last occurrence of the specified character, searching backward starting at the specified index. |
public int lastIndexOf(String str) |
It returns the index of the last occurrence of the specified substring. |
public int lastIndexOf(String str, int fromIndex) |
It returns the index of the last occurrence of the specified substring, searching backward starting at the specified index. |
Parameters
- ch- Character value to be searched
- fromIndex- index position from where the index of the char value or substring is returned.
- str- substring to be searched
Returns
- last index of char or substring.
Java String lastIndexOf() Examples
The following examples demonstrate how to use lastIndexOf() method in Java:
Example:
To show the working of the lastIndexOf() method.
Java
public class LastIndexOfExample {
public static void main(String[] args) {
String str = "GeeksforGeeks" ;
char ch = 'G' ;
int lastIndex = str.lastIndexOf(ch);
System.out.println( "Last index of '" + ch + "': " + lastIndex);
}
}
|
Output
Last index of 'G': 8
Java String lastIndexOf(char ch) Method Example:
To show the working of the lastIndexOf(char ch) method.
Java
public class L_index1 {
public static void main(String args[])
{
String Str = new String("Welcome to geeksforgeeks");
System.out.print("Found Last Index of g at : ");
System.out.println(Str.lastIndexOf( 'g' ));
}
}
|
Output:
Found Last Index of g at : 19
Java String lastIndexOf(char ch, int fromIndex) Method Example:
To show the working of the lastIndexOf(char ch, int fromIndex) method.
Java
public class L_index2 {
public static void main(String args[])
{
String Str = new String("Welcome to geeksforgeeks");
System.out.print("Found Last Index of g at : ");
System.out.println(Str.lastIndexOf( 'g' , 15 ));
}
}
|
Output:
Found Last Index of g at : 11
Java lastIndexOf(String str) Example:
To show the working of the lastIndexOf(String str) method.
Java
public class L_index3 {
public static void main(String args[])
{
String Str = new String("Welcome to geeksforgeeks");
System.out.print("Found substring geeks at : ");
System.out.println(Str.lastIndexOf("geeks"));
}
}
|
Output:
Found substring geeks at : 19
Java String lastIndexOf(String str, int fromIndex) Method Example
To show the working of the lastIndexOf(String str, int fromIndex) method.
Java
public class L_index4 {
public static void main(String args[])
{
String Str = new String("Welcome to geeksforgeeks");
System.out.print("Found substring geeks at : ");
System.out.println(Str.lastIndexOf("geeks", 15 ));
}
}
|
Output:
Found substring geeks at : 11
References
To know more about more String Methods refer to the article Java String Methods
Whether you are a beginner starting Java programming or an experienced looking to brush up on your Java skills, this tutorial will provide you with a deep understanding of the lastIndexOf function and its uses in Java.
The charAt method in Java is a fundamental function for string manipulation. With this guide, you can easily get the last index of a substring using the lastIndexOf function.
Last Updated :
16 Feb, 2024
Like Article
Save Article
Share your thoughts in the comments
Please Login to comment...