BeginnersBook

  • Home
  • Java
    • Java OOPs
    • Java Collections
    • Java Examples
  • C
    • C Examples
  • C++
    • C++ Examples
  • DBMS
  • Computer Network
  • Python
    • Python Examples
  • More…
    • jQuery
    • Kotlin
    • WordPress
    • SEO
    • JSON
    • JSP
    • JSTL
    • Servlet
    • MongoDB
    • XML
    • Perl

Java String lastIndexOf() Method with example

Last Updated: September 11, 2022 by Chaitanya Singh | Filed Under: java

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 ❯

Top Related Articles:

  1. Java String equals() and equalsIgnoreCase() Methods example
  2. Java String startsWith() Method with examples
  3. Java StringBuilder indexOf()
  4. Java StringBuffer length()
  5. Java String charAt() Method example

Tags: Java-Strings

About the Author

I have 15 years of experience in the IT industry, working with renowned multinational corporations. Additionally, I have dedicated over a decade to teaching, allowing me to refine my skills in delivering information in a simple and easily understandable manner.

– Chaitanya

Leave a Reply Cancel reply

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

Java Tutorial

Java Introduction

  • Java Index
  • Java Introduction
  • History of Java
  • Features of Java
  • C++ vs Java
  • JDK vs JRE vs JVM
  • JVM - Java Virtual Machine
  • First Java Program
  • Variables
  • Data Types
  • Operators

Java Flow Control

  • Java If-else
  • Java Switch-Case
  • Java For loop
  • Java while loop
  • Java do-while loop
  • Continue statement
  • break statement

Java Arrays

  • Java Arrays

OOPs Concepts

  • OOPs Concepts
  • Constructor
  • Java String
  • Static keyword
  • Inheritance
  • Types of inheritance
  • Aggregation
  • Association
  • Super Keyword
  • Method overloading
  • Method overriding
  • Overloading vs Overriding
  • Polymorphism
  • Types of polymorphism
  • Static and dynamic binding
  • Abstract class and methods
  • Interface
  • Abstract class vs interface
  • Encapsulation
  • Packages
  • Access modifiers
  • Garbage Collection
  • Inner classes
  • Static import
  • Static constructor

Java Exception Handling

  • Exception handling
  • Java try-catch
  • Java throw
  • Java throws
  • Checked and Unchecked Exceptions
  • Jav try catch finally
  • Exception Examples
  • Exception Propagation

Collections Framework

  • Collections in Java
  • Java ArrayList
  • Java LinkedList
  • Java Vector
  • Java HashSet
  • Java LinkedHashSet
  • Java TreeSet
  • Java HashMap
  • Java TreeMap
  • Java LinkedHashMap
  • Java Queue
  • Java PriorityQueue
  • Java Deque
  • Comparable interface
  • Comparator interface
  • Collections Interview Questions

MORE ...

  • Java Scanner Class
  • Java 8 Features
  • Java 9 Features
  • Java Conversion
  • Java Date
  • Java Multithreading
  • Java I/O
  • Java Serialization
  • Java Regex
  • Java AWT
  • Java Swing
  • Java Enum
  • Java Annotations
  • Java main method
  • Java Interview Q

Copyright © 2012 – 2025 BeginnersBook . Privacy Policy . Sitemap