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

By Chaitanya Singh | Filed Under: java

Java StringBuffer lastIndexOf() method returns the index of last occurrence of the given string in this sequence. In this tutorial, we will discuss the lastIndexOf() method with examples.

Syntax of lastIndexOf() method

//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 StringBuffer class.

lastIndexOf() Description

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

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

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

lastIndexOf() Parameters

  • str: The String str represents the character sequence that needs to be searched in the StringBuffer 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: Find last Occurrence of a given string

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

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

Output:

Java StringBuffer lastIndexOf() Output 1

Example 2: Search string before a given index

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

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

Output:

Java StringBuffer lastIndexOf() Output 2

Example 3: If search string is not found

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

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

Output:

Java StringBuffer lastIndexOf() Output 3

Example 4: If given string is not found before given index

public class JavaExample {
  public static void main(String[] args) {
    StringBuffer sb = new StringBuffer("My Cat and Rat");
    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 StringBuffer lastIndexOf() Output 4

Recommended Posts

  • Java StringBuffer length()
  • Java StringBuffer trimToSize()
  • Java StringBuffer delete()
  • Java StringBuffer insert()
❮ Java StringBuffer class

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