Java StringBuilder lastIndexOf() Method

The StringBuilder.lastIndexOf() method in Java is used to find the index of the last occurrence of a specified substring within a StringBuilder object. This guide will cover both overloaded versions of the method, explain how they work, and provide examples to demonstrate their functionality. We will also cover a real-world use case to show how StringBuilder.lastIndexOf() can be used effectively.

Table of Contents

  1. Introduction
  2. lastIndexOf Method Syntax
  3. Examples
    • Finding the Last Index of a Substring
    • Finding the Last Index of a Substring from a Specific Position
  4. Real-World Use Case
    • Example: Finding the Last Occurrence of a Word in a Paragraph
  5. Conclusion

Introduction

The StringBuilder.lastIndexOf() method is a member of the StringBuilder class in Java. It allows you to find the index of the last occurrence of a specified substring within a StringBuilder object. There are two overloaded versions of this method: one that searches the entire StringBuilder and another that starts the search from a specified index and searches backwards.

lastIndexOf Method Syntax

lastIndexOf(String str)

The syntax for the lastIndexOf(String str) method is as follows:

public 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.

lastIndexOf(String str, int fromIndex)

The syntax for the lastIndexOf(String str, int fromIndex) method is as follows:

public int lastIndexOf(String str, int fromIndex)
  • Parameters:
    • str: The substring to search for.
    • fromIndex: The index to start the search from, searching backwards.
  • Returns: The index of the last occurrence of the specified substring starting from the specified index, or -1 if there is no such occurrence.

Examples

Finding the Last Index of a Substring

The lastIndexOf(String str) method can be used to find the last occurrence of a substring within a StringBuilder object.

Example

public class LastIndexOfExample {
    public static void main(String[] args) {
        StringBuilder sb = new StringBuilder("Hello, world! Hello, universe!");

        // Finding the last index of the substring "Hello"
        int lastIndex = sb.lastIndexOf("Hello");

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

Output:

Last index of 'Hello': 14

Finding the Last Index of a Substring from a Specific Position

The lastIndexOf(String str, int fromIndex) method can be used to find the last occurrence of a substring starting from a specified index and searching backwards within a StringBuilder object.

Example

public class LastIndexOfFromIndexExample {
    public static void main(String[] args) {
        StringBuilder sb = new StringBuilder("Hello, world! Hello, universe!");

        // Finding the last index of the substring "Hello" starting from index 10
        int lastIndex = sb.lastIndexOf("Hello", 10);

        // Printing the last index
        System.out.println("Last index of 'Hello' from index 10: " + lastIndex);
    }
}

Output:

Last index of 'Hello' from index 10: 0

Real-World Use Case

Example: Finding the Last Occurrence of a Word in a Paragraph

A common real-world use case for StringBuilder.lastIndexOf() is finding the last occurrence of a word within a paragraph.

Example

public class FindLastWordOccurrence {
    public static void main(String[] args) {
        StringBuilder paragraph = new StringBuilder("The quick brown fox jumps over the lazy dog. The fox is quick.");

        // Finding the last occurrence of the word "fox"
        int lastOccurrence = paragraph.lastIndexOf("fox");

        // Printing the position of the last occurrence
        if (lastOccurrence != -1) {
            System.out.println("The last occurrence of 'fox' is at index: " + lastOccurrence);
        } else {
            System.out.println("The word 'fox' is not found.");
        }
    }
}

Output:

The last occurrence of 'fox' is at index: 44

In this example, StringBuilder.lastIndexOf() is used to find the last occurrence of the word "fox" within a paragraph, demonstrating how it can be useful for text searching and processing tasks.

Conclusion

The StringBuilder.lastIndexOf() method in Java provides a way to find the last occurrence of a specified substring within a StringBuilder object. By understanding how to use both overloaded versions of this method, you can efficiently search for substrings in your Java applications. The method allows you to perform various string search operations, making it a versatile tool for string manipulation and text processing in various scenarios.

Comments