beginnersbook.com

  • Home
  • All Tutorials
    • Learn Servlet
    • Learn JSP
    • Learn JSTL
    • Learn C
    • Learn C++
    • Learn MongoDB
    • Learn XML
    • Learn Python
    • Learn Perl
    • Learn Kotlin
  • Core Java
  • OOPs
  • Collections
  • Java I/O
  • JSON
  • DBMS

Java String lastIndexOf() Method with example

By Chaitanya Singh | Filed Under: String handling

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:
Java String lastIndexOf() method example

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
❮ PreviousNext ❯

Leave a Reply Cancel reply

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

Java Tutorials

  • Learn Java
  • OOPs Concepts
  • Java Collections

Java String

  • Java String

Java String Methods

  • String charAt()
  • String compareTo()
  • String compareToIgnoreCase()
  • String contains()
  • String concat()
  • substring
  • String valueOf()
  • String startsWith()
  • String equals()
  • String format()
  • String endsWith()
  • String indexOf()
  • String lastIndexOf()
  • String length()
  • String replace()
  • String split()
  • String trim()
  • String intern()
  • String isEmpty()
  • String matches()
  • String regionMatches()
  • String contentEquals()
  • String toCharArray()
  • String getBytes()
  • String join()
  • String getChars()
  • String copyValueOf()

Recently Added..

  • JSON Tutorial
  • Java Regular Expressions Tutorial
  • Java Enum Tutorial
  • Java Annotations Tutorial

Copyright © 2012 – 2019 BeginnersBook . Privacy Policy . Sitemap