Home » Java programming language

Java StringBuffer int lastIndexOf(String s) method with Example

Java StringBuffer int lastIndexOf(String s) method: Here, we are going to learn about the int lastIndexOf(String s) method of StringBuffer class with its syntax and example.
Submitted by Preeti Jain, on July 02, 2019

StringBuffer Class int lastIndexOf(String s)

  • This method is available in package java.lang.StringBuffer.lastIndexOf(String s).
  • This method is used to return the index of the last occurrence of the specified substring which will be searched in a StringBuffer object.
  • This method returns -1 if the substring is not found in the given StringBuffer object.

Syntax:

    int lastIndexOf(String s){
    }

Parameter(s):

We pass only one object in the method of the StringBuffer i.e. String.

Return value:

The return type of this method is int that means this method returns the index of the last occurrence of specified substring and i.e. index is in integer form that's why return type is int.

Java program to demonstrate example of lastIndexOf(String s) method

Case 1: How int lastIndexOf(String s) method works with duplicate(Multiple times) substring in the StringBuffer object?

import java.lang.StringBuffer;

public class StringBufferClass {
    public static void main(String[] args) {

        StringBuffer sb = new StringBuffer("java.utilisapackageofJava");

        // use lastIndexOf(String s) it will retrieve 
        // the index of last occurrence of specified 
        // substring from the StringBuffer object .

        // Display result after implementing lastIndexOf("v") i.e. v 
        // comes twice in a stringbuffer object so it returns the index 
        // of last occurrence of v i.e. 23rd index
        // [ First index of v is 2nd and Second index of v is 23rd ]
        System.out.println("The index of last occurrence of v is :" + sb.lastIndexOf("v"));
    }
}

Output

D:\Programs>javac StringBufferClass.java

D:\Programs>java StringBufferClass
The index of last occurrence of v is : 23

Case 2: How int lastIndexOf(String s) method works without duplicate(i.e. Multiple times) substring in the StringBuffer object?

import java.lang.StringBuffer;

public class StringBufferClass {
    public static void main(String[] args) {

        StringBuffer sb = new StringBuffer("Javaispopularinthisworld");

        // use lastIndexOf(String s) it will retrieve the index 
        // of last occurrence of specified substring 
        // from the StringBuffer object .

        // Display result after implementing lastIndexOf("v") 
        // i.e. v comes once in a stringbuffer object so 
        // it returns the index of last occurrence of v 
        // i.e. 2nd index[ First and Last index of v is one and only one 2nd position]
        System.out.println("The index of last occurrence of v is :" + sb.lastIndexOf("v"));
    }
}

Output

D:\Programs>javac StringBufferClass.java

D:\Programs>java StringBufferClass
The index of last occurrence of v is :2


Comments and Discussions!

Load comments ↻





Copyright © 2024 www.includehelp.com. All rights reserved.