Java String lastIndexOf example

String's lastIndexOf method is used to find out last index of any character or sub string.

lastIndexOf method can take char or string as argument and also you can provide fromIndex also.

Methods:

public int lastIndexOf(int ch)
returns index of last occurrence of character within String

public int lastIndexOf(int ch,int fromIndex)
returns index of last occurrence of character within String, going backward from specific index "fromIndex"

public int lastIndexOf(String)
returns index of last occurrence of substring within String

public int lastIndexOf(int ch,int fromIndex)
returns index of last occurrence of substring within String, going backward from specific index "fromIndex"

All lastIndexOf methods return -1 if char or substring is not present in the String

String indexOf Example:

package org.arpit.java2blog;

public class StringLastIndexOfExample {
 /*
  * @ Author : Arpit Mandliya
  */
 public static void main(String args[]) {
  
  String str="Hello world from java2blog";
  // Using lastIndexOf method with character
  System.out.println("---------------------------------------------------");
  System.out.println("Using lastIndexOf method with character");
  System.out.println("---------------------------------------------------");
  
  System.out.println("Last index of character l in \"Hello world from java2blog\" is : "+str.lastIndexOf('l'));
  System.out.println();
  
  String str1="from";
  System.out.println("---------------------------------------------------");
  System.out.println("Using lastIndexOf method with fromIndex with character");
  System.out.println("---------------------------------------------------");
  System.out.println("Last index of character o from index 6 in \"Hello world from java2blog\" is : "+str.lastIndexOf('o',6));
  System.out.println();
  
  // Using lastIndexOf method with String
  System.out.println("---------------------------------------------------");
  System.out.println("Using lastIndexOf method with String");
  System.out.println("---------------------------------------------------");
  
  System.out.println("Last index of String \"from\" in \"Hello world from java2blog\" is : "+str.lastIndexOf(str1));
  System.out.println();
  
  String howDo="How do you do";
  System.out.println("---------------------------------------------------");
  System.out.println("Using lastIndexOf method with fromIndex with String");
  System.out.println("---------------------------------------------------");
  System.out.println("Last index of String \"do\" from index 9 in \"How do you do\" is : "+howDo.lastIndexOf("do",9));
  System.out.println();
  
  System.out.println("---------------------------------------------------");
  System.out.println("If String or char is not available");
  System.out.println("---------------------------------------------------");
  System.out.println("Last index of \"from\" in \"How do you do\" is : "+howDo.lastIndexOf("from"));
  
  
 }
}

When you run above program, you will get below output:
--------------------
---------------------------------------------------
Using lastIndexOf method with character
---------------------------------------------------
Last index of character l in "Hello world from java2blog" is : 23

---------------------------------------------------
Using lastIndexOf method with fromIndex with character
---------------------------------------------------
Last index of character o from index 6 in "Hello world from java2blog" is : 4

---------------------------------------------------
Using lastIndexOf method with String
---------------------------------------------------
Last index of String "from" in "Hello world from java2blog" is : 12

---------------------------------------------------
Using lastIndexOf method with fromIndex with String
---------------------------------------------------
Last index of String "do" from index 9 in "How do you do" is : 4

---------------------------------------------------
If String or char is not available
---------------------------------------------------
Last index of "from" in "How do you do" is : -1


Written by Arpit:

If you have read the post and liked it. Please connect with me on Facebook | Twitter | Google Plus

 

Java tutorial for beginners Copyright © 2012