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 StringBuilder lastIndexOf()

By Chaitanya Singh | Filed Under: java

In this tutorial, we will discuss the Java StringBuilder lastIndexOf() method with the help of example programs.

The syntax of lastIndexOf() method is:

//Returns the index of last occurrence of string "welcome"
sb.lastIndexOf("welcome");

//Returns index of last occurrence of "welcome" before index 5
sb.lastIndexOf("welcome", 5);

Here, sb is an object of StringBuilder class.

lastIndexOf() Description

There are two variations of lastIndexOf() method in Java StringBuilder class.

public int lastIndexOf(String str): It returns the index of last occurrence of the string str in the StringBuilder instance.

public int lastIndexOf(String str, int fromIndex): It returns the index of last occurrence of specified string before the fromIndex.

To get index of first occurrence of specified string use Java StringBuilder indexOf() method.

lastIndexOf() Parameters

The lastIndexOf() method of Java StringBuilder class takes a single parameter:

  • str: The String str represents the character sequence that needs to be searched in the StringBuilder instance sb.
  • fromIndex: It represents an index. The search of the string str ends here.

lastIndexOf() Return Value

  • It returns an integer value that represents the index of the last occurrence of specified string str. If the specified string is not found then this method returns -1.

Example 1: Search string without fromIndex

public class JavaExample {
  public static void main(String[] args) {
    StringBuilder sb = new StringBuilder("Text1Text2");
    System.out.println("String: " + sb);

    // last occurrence of string "Text"
    System.out.println("Last Occurrence of String 'Text': "+
            sb.lastIndexOf("Text"));
  }
}

Output:

Java StringBuilder lastIndexOf Output_1

Example 2: Search string before an index

public class JavaExample {
  public static void main(String[] args) {
    StringBuilder sb = new StringBuilder("BatRat");
    System.out.println("String: " + sb);

    // last occurrence of string "at" before 3
    System.out.println("Last Occurrence of 'at' before index 3: "+
            sb.lastIndexOf("at", 3));
  }
}

Output:

Java StringBuilder lastIndexOf Output_2

Example 3: When specified string is not found

public class JavaExample {
  public static void main(String[] args) {
    StringBuilder sb = new StringBuilder("BatRat");
    System.out.println("String: " + sb);

    // last occurrence of string "Pet"
    System.out.println("Last Occurrence of 'Pet': "+
            sb.lastIndexOf("Pet"));
  }
}

Output:

Java StringBuilder lastIndexOf Output_3

Example 4: When string not found within specified Index

public class JavaExample {
  public static void main(String[] args) {
    StringBuilder sb = new StringBuilder("BatRat");
    System.out.println("String: " + sb);

    // last occurrence of string "at" before 0
    System.out.println("Last Occurrence of 'at' before index 0: "+
            sb.lastIndexOf("at", 0));
  }
}

Output:

Java StringBuilder lastIndexOf Output_4

Recommended Posts

  • Java StringBuilder ensureCapacity()
  • Java StringBuilder substring()
  • Java StringBuilder charAt()

Top Related Articles:

  1. Java StringBuilder Class With Examples
  2. Java StringBuilder insert()
  3. Java StringBuilder append() Method
  4. Java String lastIndexOf() Method with example
  5. Java StringBuffer lastIndexOf()

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

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 – 2023 BeginnersBook . Privacy Policy . Sitemap