Java String lastIndexOf() Method with Examples (original) (raw)
Last Updated : 19 Nov, 2024
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.
In this article, we will learn how to use the **string lastIndexOf()
method in Java to find the last occurrence of a character or substring in a String
.
Program to Find the Last Index of a Character in a String in Java
This program demonstrates how to use the lastIndexOf()
method to find the position of the last occurrence of a specified character in a given string.
Java `
// Java program to demonstrate the lastIndexOf(char ch) method public class L_index1 { public static void main(String args[]) { // Creating a string String s = new String("Hello World");
// Printing message before finding last index of 'l'
System.out.print("Found Last Index of l at: ");
// Finding and printing the last index of 'l'
System.out.println(s.lastIndexOf('l'));
}
}
`
Output
Found Last Index of l at: 9
Table of Content
Different Ways to Use lastIndexOf() Method in Java
There are **four variants of the lastIndexOf() method in Java.
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.
**Return Value:
- Last index of char or substring.
Now, lets see examples of all 4 variants of lastIndexOf() method of String class.
Using lastIndexOf(char ch) Method
To demonstrate how to find the last index of a character in a string.
Java `
// Java program to demonstrate the lastIndexOf(char ch) method public class LastIndexOfExample { public static void main(String[] args) {
// Define a string and a character to search for
String s = "GeeksforGeeks";
char ch = 'G';
// Search for the last occurrence of the character in the string
int l = s.lastIndexOf(ch);
// Print the last index of the character
System.out.println("Last index of '" + ch + "': " + l);
}
}
`
Output
Last index of 'G': 8
Finding the Last Index of a Character Example
To illustrate the last index of āgā in a specific string.
Java `
// Java Program to demonstrate the working of lastIndexOf() public class L_index1 {
public static void main(String args[]) { // Initializing String String s = new String("Welcome to geeksforgeeks");
System.out.print("Found Last Index of g at: ");
System.out.println(s.lastIndexOf('g'));
}
}
`
Output
Found Last Index of g at: 19
Using lastIndexOf(char ch, int fromIndex) Method
To show how to find the last index of a character, searching backwards from a specified index.
Java `
// Java Program to demonstrate the working of lastIndexOf(char ch, int fromIndex) Method public class L_index2 {
public static void main(String args[]) {
// Initializing String
String s = new String("Welcome to geeksforgeeks");
System.out.print("Found Last Index of g at: ");
System.out.println(s.lastIndexOf('g', 15));
}
}
`
Output
Found Last Index of g at: 11
Using lastIndexOf(String str) Method
To demonstrate how to find the last index of a substring in a string.
Java `
// Java Program to demonstrate the working of lastIndexOf(String str) public class L_index3 {
public static void main(String args[]) {
// Initializing String
String s = new String("Welcome to geeksforgeeks");
System.out.print("Found substring geeks at: ");
System.out.println(s.lastIndexOf("geeks"));
}
}
`
Output
Found substring geeks at: 19
Using lastIndexOf(String str, int fromIndex) Method
To show how to find the last index of a substring, searching backwards from a specified index.
Java `
// Java Program to demonstrate the working of lastIndexOf(String str, int fromIndex) public class L_index4 {
public static void main(String args[]) {
// Initializing String
String s = new String("Welcome to geeksforgeeks");
System.out.print("Found substring geeks at: ");
// start index of last occurrence of 'geeks' before 15
System.out.println(s.lastIndexOf("geeks", 15));
}
}
`
Output
Found substring geeks at: 11