Java StringBuffer lastIndexOf() Method

The StringBuffer.lastIndexOf() method in Java is used to find the index of the last occurrence of a specified substring within the StringBuffer object. This guide will cover the method's usage, explain how it works, and provide examples to demonstrate its functionality.

Table of Contents

  1. Introduction
  2. lastIndexOf Method Syntax
  3. Examples
    • Finding the Last Occurrence of a Substring
    • Finding the Last Occurrence of a Substring from a Specified Index
  4. Conclusion

Introduction

The lastIndexOf() method is a member of the StringBuffer class in Java. It allows you to search for the last occurrence of a substring within the StringBuffer and returns the index of that occurrence. If the substring is not found, the method returns -1.

lastIndexOf Method Syntax

The syntax for the lastIndexOf method is as follows:

public synchronized int lastIndexOf(String str)

Parameters:

  • str - the substring to search for.

Returns:

  • The index of the last occurrence of the specified substring, or -1 if there is no such occurrence.
public synchronized int lastIndexOf(String str, int fromIndex)

Parameters:

  • str - the substring to search for.
  • fromIndex - the index to start the search backward from.

Returns:

  • The index of the last occurrence of the specified substring, searching backward starting at the specified index, or -1 if there is no such occurrence.

Examples

Finding the Last Occurrence of a Substring

The lastIndexOf method can be used to find the index of the last occurrence of a specified substring within the StringBuffer object.

Example

public class StringBufferLastIndexOfExample {
    public static void main(String[] args) {
        // Create a StringBuffer object with initial content
        StringBuffer sb = new StringBuffer("Hello, World! Hello, Universe!");

        // Find the index of the last occurrence of "Hello"
        int index = sb.lastIndexOf("Hello");

        // Print the index
        System.out.println("Last index of 'Hello': " + index);
    }
}

Output:

Last index of 'Hello': 14

Finding the Last Occurrence of a Substring from a Specified Index

The lastIndexOf method can also be used to find the index of the last occurrence of a specified substring, starting the search backward from a given index.

Example

public class StringBufferLastIndexOfExample {
    public static void main(String[] args) {
        // Create a StringBuffer object with initial content
        StringBuffer sb = new StringBuffer("Hello, World! Hello, Universe!");

        // Find the index of the last occurrence of "Hello" starting from index 12
        int index = sb.lastIndexOf("Hello", 12);

        // Print the index
        System.out.println("Last index of 'Hello' from index 12: " + index);
    }
}

Output:

Last index of 'Hello' from index 12: 0

Conclusion

The StringBuffer.lastIndexOf() method in Java provides a way to find the index of the last occurrence of a specified substring within a StringBuffer object. By understanding how to use this method, you can efficiently search for substrings from the end of the StringBuffer and manage text data. This method is particularly useful for applications that require searching and locating specific patterns or substrings within larger text sequences.

Comments