Java StringBuffer indexOf() Method

The StringBuffer.indexOf() method in Java is used to find the index of the first 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. indexOf Method Syntax
  3. Examples
    • Finding the First Occurrence of a Substring
    • Finding the First Occurrence of a Substring from a Specified Index
  4. Conclusion

Introduction

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

indexOf Method Syntax

The syntax for the indexOf method is as follows:

public synchronized int indexOf(String str)

Parameters:

  • str - the substring to search for.

Returns:

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

Parameters:

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

Returns:

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

Examples

Finding the First Occurrence of a Substring

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

Example

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

        // Find the index of the first occurrence of "World"
        int index = sb.indexOf("World");

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

Output:

Index of 'World': 7

Finding the First Occurrence of a Substring from a Specified Index

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

Example

public class StringBufferIndexOfExample {
    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 first occurrence of "Hello" starting from index 10
        int index = sb.indexOf("Hello", 10);

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

Output:

Index of 'Hello' from index 10: 14

Conclusion

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

Comments