String lastIndexOf() in Java

String lastIndexOf() Method in Java | In this section, we will discuss lastIndexOf() method of the Java String class. The String lastIndexOf() method is a built-in method that returns the last index or occurrence of the given character or substring.

For the lastIndexOf() method the index counter starts from 0, and if the character or substring is not found it returns -1. Similar to the lastIndexOf() method, the Java string class also contains the indexOf() method.

Examples of the Java String lastIndexOf method are as follows:-
Example-1:-
string = “Good Morning”
Character:- ‘g’
The last index of g is 11.

Example-2:-
String = “Happy day”
Character:- ‘z’
The character ‘z’ is not found in the given string hence the result is -1.

Example-3:-
String = “It was a great day”
Substring:- “great”
The substring great is present at index 9.

There are four variations in the lastIndexOf() method. The syntax of the Java lastIndexOf() String is as follows:-

  • public int lastIndexOf(int char1)
  • public int lastIndexOf(int char1, int fromIndex)
  • public int lastIndexOf(String substring)
  • public int lastIndexOf(String substring, int fromIndex)

Parameters,
Char1: the character needed to be searched.
Substring: the substring needed to be searched
fromIndex: this specifies the method to start the search from the given index.

Returns:- The index of the character/string. -1 if character/substring is not found.
Return Type: int

Java String lastIndexOf() Example

Let us see an example for the public int lastIndexOf(int char1) method. We will take a character that exists multiple times and one character which doesn’t exist.

public class Main {
   public static void main(String args[]) {
      
      String string = "Have a great day!";
      char ch1 = 'a';
      char ch2 = 'X';
      
      int lastIndex1 = string.lastIndexOf(ch1);
      int lastIndex2 = string.lastIndexOf(ch2);
      
      System.out.println("Last Index of \'" + ch1 
            + "\' at = " + lastIndex1);
      System.out.println("Last Index of \'" + ch2 
            + "\' at = " + lastIndex2);
   }
}

Output:-

Last Index of ‘a’ at = 14
Last Index of ‘X’ at = -1

Let us see an example for the public int lastIndexOf(int char1, int fromIndex) method. It will return the index of the last character from the given index position.

public class Main {
   public static void main(String args[]) {
      String str = "Hello people";
      System.out.println(str.lastIndexOf('e', str.length()));
      System.out.println(str.lastIndexOf('e', 9));
      System.out.println(str.lastIndexOf('e', 5));
   }
}

Output:-

11
7
1

From the above program, we can understand that calling str.lastIndexOf(‘e’, str.length()) & str.lastIndexOf(‘e’) will always produce the same result.

In the public int lastIndexOf(int char1, int fromIndex) method if we pass the second parameter <0 then we will always get -1, because there is no character/string before the 0th index, and it will not search in circular order.

public class Main {
   public static void main(String args[]) {
      String str = "Hello people";
      System.out.println(str.lastIndexOf('e', -1));
      System.out.println(str.lastIndexOf('e', -9));
      System.out.println(
          str.lastIndexOf('e', Integer.MIN_VALUE));
   }
}

Output:-

-1
-1
-1

String lastIndexOf() Java Example

We have already seen the lastIndexOf() example for the character now let us see examples for the sub-string. The method signature is- public int lastIndexOf(String substring).

Since Java is a case-sensitive programming language therefore it will compare case-wise. It will return the last index of the given sub-string from the given string. If the sub-string is not available then it returns -1.

public class Main {
   public static void main(String args[]) {
      String str = "Welocome to Knowprogram. "
         + "Java is one of the popular programming language";

      System.out.println(str.lastIndexOf("program"));
      System.out.println(str.lastIndexOf("one"));
      System.out.println(str.lastIndexOf("Hello"));
   }
}

Output:-

52
33
-1

Now let us see an example for the public int lastIndexOf(String substring, int fromIndex) method. Here also, if the passed second argument is <0 then we will always get -1 because it won’t compare in circular order.

public class Main {
   public static void main(String args[]) {
      String str = "Fear leads to anger;"
            + " anger leads to hatred;"
            + " hatred leads to conflict;"
            + " conflict leads to suffering.";

      System.out.println(str.lastIndexOf("leads", str.length()));
      System.out.println(str.lastIndexOf("leads", 70));
      System.out.println(str.lastIndexOf("leads", 41));
      System.out.println(str.lastIndexOf("leads", 17));
      System.out.println(str.lastIndexOf("leads", 4));
   }
}

Output:-

79
51
27
5
-1

Java lastIndexOf() Quiz

Q1) Which of these will give the same result for String name = "KnowProgram"?

a) name.lastIndexOf('o')
b) name.lastIndexOf('o', name.length())
c) name.lastIndexOf('o', Integer.MAX_VALUE)
d) name.lastIndexOf('o', Integer.MIN_VALUE)

View Answer Answer:- (a), (b), & (c)

Q2) Find the output of the below program?

public class Main {
   public static void main(String args[]) {
      String name = "KnowProgram";
      System.out.println(name.lastIndexOf("KnowProgram"));
   }
}

a) 1
b) 0
c) -1
d) None of these

View Answer Answer:- (b)

Explanation:- The index starts from 0. The sub-string “KnowProgram” starts from 0th index in the given string “KnowProgram”. Therefore program will return 0 as output.

Q3) Find the output of the below program.

public class Main {
   public static void main(String args[]) {
      String name = "KnowProgram";
      System.out.println(name.lastIndexOf("o", -0));
   }
}

a) 1
b) 0
c) -1
d) None of these

View Answer Answer:- (c)

If you enjoyed this post, share it with your friends. Do you want to share more information about the topic discussed above or do you find anything incorrect? Let us know in the comments. Thank you!

Leave a Comment

Your email address will not be published. Required fields are marked *