In the last tutorial we discussed indexOf()
method, which is used to find out the occurrence of a specified char or a substring in the given String. In this tutorial we will discuss lastIndexOf()
method which is used to find out the index of last occurrence of a character or a substring in a given String.
Java String lastIndexOf() Method Signature
To find out the last occurrence of the specified character or a string, this method starts the search from the end of string and proceeds backwards from there. If the fromIndex
is specified during the method call, then the backward search begins from the specified index fromIndex
int lastIndexOf(int ch)
: It returns the last occurrence of character ch in the given String.
int lastIndexOf(int ch, int fromIndex)
: It returns the last occurrence of ch, it starts looking backwards from the specified index “fromIndex”.
int lastIndexOf(String str)
: Returns the last occurrence of substring str in a String.
int lastIndexOf(String str, int fromIndex)
: Returns the last occurrence of str, starts searching backward from the specified index “fromIndex”.
Java String lastIndexOf() example
In the following example we are searching few given characters and a given substring in the String str
. We are looking for their last occurrence in the String str thats why we are using lastIndexOf() method. If you want to find out the first occurrence of a char or substring in a string then using indexOf() method instead.
public class JavaExample { public static void main(String[] args) { String str = "beginnersbook is for beginners"; char ch = 'b'; char ch2 = 's'; String subStr = "beginners"; int posOfB = str.lastIndexOf(ch); int posOfS = str.lastIndexOf(ch2); int posOfSubstr = str.lastIndexOf(subStr); System.out.println(posOfB); System.out.println(posOfS); System.out.println(posOfSubstr); } }
Output:
Another example of String lastIndexOf() Method
Here we are demonstrating the use of lastIndexOf() method in various cases. We are trying the different variants of the lastIndexOf() method where we provide the fromIndex
and the method then searches backwards from the specified fromIndex
.
public class LastIndexOfExample{ public static void main(String args[]) { String str1 = new String("This is a BeginnersBook tutorial"); String str2 = new String("Beginners"); String str3 = new String("Book"); String str4 = new String("Books"); System.out.println("Last 'B' in str1: "+str1.lastIndexOf('B')); System.out.println("Last 'B' in str1 whose index<=15:"+str1.lastIndexOf('B', 15)); System.out.println("Last 'B' in str1 whose index<=30:"+str1.lastIndexOf('B', 30)); System.out.println("Last occurrence of str2 in str1:"+str1.lastIndexOf(str2)); System.out.println("Last occurrence of str2 in str1 before 15:"+str1.lastIndexOf(str2, 15)); System.out.println("Last occurrence of str3 in str1:"+str1.lastIndexOf(str3)); System.out.println("Last occurrence of str4 in str1"+str1.lastIndexOf(str4)); System.out.println("Last occurrence of 'is' in str1:"+str1.lastIndexOf("is")); System.out.println("Last occurrence of 'is' in str1 before 4:"+str1.lastIndexOf("is", 4)); } }
Output:
Last 'B' in str1: 19 Last 'B' in str1 whose index<=15:10 Last 'B' in str1 whose index<=30:19 Last occurrence of str2 in str1:10 Last occurrence of str2 in str1 before 15:10 Last occurrence of str3 in str1:19 Last occurrence of str4 in str1-1 Last occurrence of 'is' in str1:5 Last occurrence of 'is' in str1 before 4:2
Leave a Reply