String lastIndexOf() method examples in Java
The String class contains many useful methods for manipulating the String.
The lastIndexOf is the method of String class which returns the index of the last occurrence of the specified character/substring within the string.
'null' argument is not allowed.
Passing null argument is not allowed to lastIndexOf() method. It will result in a NullPointerException exception.
There are 4 overloaded methods of the lastIndexOf method as given below. We will see them. 1). int lastIndexOf(int ch)
Returns the index within this string of the last occurrence of the specified character.
2). int lastIndexOf(int ch, int fromIndex)
Returns the index within this string of the last occurrence of the specified character, searching backward starting at the specified index.
Returns the index within this string of the last occurrence of the specified substring.
4). int lastIndexOf(String str, int fromIndex)
Returns the index within this string of the last occurrence of the specified substring, searching backward starting at the specified index.
lastIndexOf method parameters
1). ch - the character whose last index is to be found index (optional) - if the index is passed, the ch character is searched from start to this index To find the last index of the specified substring within the string, lastIndexOf() takes these two parameters:
2). str - the string whose last index is to be found
index (optional) - if the index is passed, the str string is searched from start to this index.
3). fromIndex
if we pass the value for fromIndex, then the ch character is searched from start to this index.
Overloaded Methods of LastIndexOf
1). int lastIndexOf(int ch)
Returns the index within this string of the last occurrence of the specified character.
public class LastIndexOfExample1 {
public static void main(String[] args) {
String str1 = "hello world";
String str2 = "java programming tutorials";
System.out.println(str1.lastIndexOf("world")); //6
System.out.println(str2.lastIndexOf("java")); //0
System.out.println(str1.lastIndexOf("java")); //-1
}
}
output:
6
0
-1
2). int lastIndexOf(int ch, int fromIndex)
public class LastIndexOfExample2 {
public static void main(String[] args) {
String str1 = "hello world";
String str2 = "java programming tutorials. please read the lines carefully.";
System.out.print("we found Last Index of p at : ");
// Last index of 'p' before 40 will print 28
System.out.println(str2.lastIndexOf('p', 40));
System.out.print("we found Last Index of w at : ");
// Last index of 'w' before 15 will print 6
System.out.println(str1.lastIndexOf('w', 15));
}
}
output:
we found Last Index of p at : 28
we found Last Index of w at : 6
3). int lastIndexOf(String str)
public class LastIndexOfExample3 {
public static void main(String[] args) {
String str1 = "hello world!!! welcome to java programming world!!";
String str2 = "java programming tutorials. please read the lines carefully.";
// start index of last occurrence of "please"
System.out.println("the last index of 'please' is : " + str2.lastIndexOf("please"));
// start index of last occurrence of "java"
System.out.println("the last index of 'java' is : " + str1.lastIndexOf("java"));
}
}
output:
the last index of 'please' is : 28
the last index of 'java' is : 26
4). int lastIndexOf(String str, int fromIndex)
public class LastIndexOfExample4 {
public static void main(String[] args) {
String str1 = "hello world";
String str2 = "java programming tutorials. please read the lines carefully.";
//start index of last occurrence of "the"
// before 40
// prints 5
System.out.println(str1.lastIndexOf("lines", 40)); //-1
System.out.println(str2.lastIndexOf("p", 15)); //5
System.out.println(str2.lastIndexOf("the", 40)); //40
System.out.println(str2.lastIndexOf("programming", 6));
5
}
}
In this article, we have seen String lastIndexOf() method examples in Java. All source code in the article can be found in the GitHub repository.
0 Comments
Post a Comment