Java String lastIndexOf() Method

The String.lastIndexOf() method in Java is used to find the index of the last occurrence of a specified character or substring within a string. 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 Index of a Character
    • Finding the Last Index of a Substring
    • Starting Search from a Specific Index
    • Handling Not Found Cases
  4. Conclusion

Introduction

The String.lastIndexOf() method is a member of the String class in Java. It allows you to locate the position of the last occurrence of a character or substring within a string. This method is particularly useful for searching, parsing, and manipulating strings based on specific characters or patterns.

lastIndexOf Method Syntax

The lastIndexOf method has several overloads:

  1. Finding the last index of a character:
public int lastIndexOf(int ch)
  1. Finding the last index of a character starting from a specified index:
public int lastIndexOf(int ch, int fromIndex)
  1. Finding the last index of a substring:
public int lastIndexOf(String str)
  1. Finding the last index of a substring starting from a specified index:
public int lastIndexOf(String str, int fromIndex)

Examples

Finding the Last Index of a Character

The lastIndexOf method can be used to find the last occurrence of a specified character within a string.

Example

public class LastIndexOfExample {
    public static void main(String[] args) {
        String message = "Hello, World!";

        int lastIndexOfO = message.lastIndexOf('o');
        int lastIndexOfL = message.lastIndexOf('l');

        System.out.println("Last index of 'o': " + lastIndexOfO);
        System.out.println("Last index of 'l': " + lastIndexOfL);
    }
}

Output:

Last index of 'o': 8
Last index of 'l': 10

Finding the Last Index of a Substring

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

Example

public class LastIndexOfExample {
    public static void main(String[] args) {
        String message = "Hello, World! Hello, Java!";

        int lastIndexOfHello = message.lastIndexOf("Hello");
        int lastIndexOfJava = message.lastIndexOf("Java");

        System.out.println("Last index of 'Hello': " + lastIndexOfHello);
        System.out.println("Last index of 'Java': " + lastIndexOfJava);
    }
}

Output:

Last index of 'Hello': 14
Last index of 'Java': 21

Starting Search from a Specific Index

The lastIndexOf method can be used to start the search from a specified index.

Example

public class LastIndexOfExample {
    public static void main(String[] args) {
        String message = "Hello, World! Hello, Java!";

        int lastIndexOfOFromIndex = message.lastIndexOf('o', 10);

        System.out.println("Last index of 'o' from index 10: " + lastIndexOfOFromIndex);
    }
}

Output:

Last index of 'o' from index 10: 8

Handling Not Found Cases

The lastIndexOf method returns -1 if the specified character or substring is not found.

Example

public class LastIndexOfExample {
    public static void main(String[] args) {
        String message = "Hello, World!";

        int lastIndexOfJava = message.lastIndexOf("Java");

        System.out.println("Last index of 'Java': " + lastIndexOfJava);
    }
}

Output:

Last index of 'Java': -1

Conclusion

The String.lastIndexOf() method in Java is for locating the position of the last occurrence of characters and substrings within a string. By understanding how to use this method, you can efficiently search, parse, and manipulate strings in your Java applications. Whether you are finding the last index of a character, a substring, starting the search from a specific index, or handling cases where the character or substring is not found, the lastIndexOf method provides a reliable solution for these tasks.

Comments