Java String lastIndexOf()

In the last tutorial we discussed indexOf() method. In this tutorial we will discuss lastIndexOf() method which is used for finding out the index of last occurrence of a char/sub-string in a particular String.

The java string lastIndexOf() method returns last index of the given character value or substring. If it is not found, it returns -1. The index counter starts from zero.

Internal Implementation
public int lastIndexOf(int ch) { return lastIndexOf(ch, value.length - 1); }
  1. lastIndexOf() : This method returns the index of the last occurrence of the character in the character sequence .

    Syntax: int lastIndexOf(int ch) Parameters: ch : a character. Return Value: This method returns the index.

  2. lastIndexOf(int ch, int beg) : This method returns the index of the last occurrence of the character in the character sequence represented by this object that is less than or equal to beg, or -1 if the character does not occur before that point.

    Syntax: public int lastIndexOf(int ch, int beg) Parameters: ch : a character. beg : the index to start the search from. Returned Value: This method returns the index.

  3. lastIndexOf(String str) : This method accepts a String as an argument, if the string argument occurs one or more times as a substring within this object, then it returns the index of the first character of the last such substring is returned. If it does not occur as a substring, -1 is returned.

    Syntax: public int lastIndexOf(String str) Parameters: str : a string. Returned Value: This method returns the index.

  4. lastIndexOf(String str, int beg) : This method returns the index within this string of the last occurrence of the specified substring, searching backward starting at the specified index.

    Syntax: public int lastIndexOf(String str, int beg) Parameters beg : the index to start the search from. str : a string. Return Value This method returns the index.

Parameters

ch: char value i.e. a single character e.g. 'a'

fromIndex: index position from where index of the char value or substring is retured

substring: substring to be searched in this string

Returns

last index of the string

Example of lastIndexOf() method
public class Test { 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
Java String lastIndexOf() method example
public class Test { public static void main(String args[]) { String s1="this is index of example"; //there are 3 'i' characters in this sentence int index1=s1.lastIndexOf('i'); //returns last index of 's' char value System.out.println(index1);//8 } }


Output:
8
Java String lastIndexOf(int ch, int fromIndex) Method Example

Here, we are finding last index from the string by specifying fromIndex

public class Test { public static void main(String[] args) { String str = "This is index of example"; int index = str.lastIndexOf('s',5); System.out.println(index); } }


Output:
3
Java String lastIndexOf(String substring) Method Example

It returns the last index of the substring.

public class Test { public static void main(String[] args) { String str = "This is last index of example"; int index = str.lastIndexOf("last"); System.out.println(index); } }


Output:
8
Java String lastIndexOf(String substring, int fromIndex) Method Example

It returns the last index of the substring from the fromIndex.

public class Test { public static void main(String[] args) { String str = "This is last index of example"; int index = str.lastIndexOf("of", 30); System.out.println(index); index = str.lastIndexOf("of", 10); System.out.println(index); // -1, if not found } }


Output:
19 -1



Instagram