lastIndexOf() : 

In Java, the lastIndexOf() method is a part of the String class.

 It is used to find the index of the last occurrence of a specified substring or character within a string.

 The method returns the index of the last occurrence, or -1 if the substring or character is not found.


The general syntax of the lastIndexOf() method is:


public int lastIndexOf(String str) ;
public int lastIndexOf(String str, int fromIndex);
public int lastIndexOf(char ch) ;
public int lastIndexOf(char ch, int fromIndex);

Here's an explanation of how the lastIndexOf() method works:


lastIndexOf(String str):

This form of the method searches for the last occurrence of the specified str substring within the string.

It returns the index of the first character of the last occurrence of the substring, or -1 if the substring is not found.

lastIndexOf(String str, int fromIndex):

This form of the method searches for the last occurrence of the specified str substring within the string, starting from the fromIndex position.

It returns the index of the first character of the last occurrence of the substring, or -1 if the substring is not found.

The search starts from the fromIndex position and works towards the beginning of the string.

lastIndexOf(char ch):

This form of the method searches for the last occurrence of the specified character ch within the string.

It returns the index of the last occurrence of the character, or -1 if the character is not found.


lastIndexOf(char ch, int fromIndex):

This form of the method searches for the last occurrence of the specified character ch within the string, starting from the fromIndex position.

It returns the index of the last occurrence of the character, or -1 if the character is not found.

The search starts from the fromIndex position and works towards the beginning of the string.


Here's an example to illustrate the usage of the lastIndexOf() method:


String str = "Hello, World!";
int lastIndex1 = str.lastIndexOf("o");
 System.out.println(lastIndex1); // Output: 8
int lastIndex2 = str.lastIndexOf("o", 7); 
System.out.println(lastIndex2); // Output: 4 
int lastIndex3 = str.lastIndexOf("Java");
 System.out.println(lastIndex3); // Output: -1
int lastIndex4 = str.lastIndexOf('l');
 System.out.println(lastIndex4); // Output: 9

In the above example:

lastIndexOf("o") finds the last occurrence of the substring "o" in the string and returns the index 8.

lastIndexOf("o", 7) starts searching for the substring "o" from index 7 and finds the last occurrence at index 4.

lastIndexOf("Java") tries to find the substring "Java" in the string, but since it is not present, it returns -1.

lastIndexOf('l') finds the last occurrence of the character 'l' in the string and returns the index 9.

The lastIndexOf() method is commonly used when you need to locate the position of the last occurrence of a substring or character within a string, allowing you to perform various string manipulation and search operations in Java.


Here are some test cases you can consider when using the lastIndexOf() method in Java:


Basic substring existence:

Input: "Hello, World!", substring: "o"

Expected output: 8

Explanation: The last occurrence of the substring "o" is found at index 8.


Substring not found:

Input: "Hello, World!", substring: "Java"

Expected output: -1

Explanation: The substring "Java" is not present in the given string, so the method returns -1.


Case sensitivity:

Input: "Hello, World!", substring: "O"

Expected output: -1

Explanation: The lastIndexOf() method is case-sensitive, so the uppercase substring "O" is not found in the given string.


Starting index specified:

Input: "Hello, World!", substring: "o", starting index: 7

Expected output: 4

Explanation: The search starts from index 7 and moves towards the beginning of the string, finding the last occurrence of the substring "o" at index 4.


Character occurrence:

Input: "Hello, World!", character: 'l'

Expected output: 9

Explanation: The last occurrence of the character 'l' is found at index 9.


Empty string:

Input: "", substring: "Hello"

Expected output: -1

Explanation: An empty string does not contain any substring, so the method returns -1.


Empty substring:

Input: "Hello, World!", substring: ""

Expected output: 13

Explanation: An empty substring is considered to be found at the last index of any non-empty string, so the method returns the length of the string (13 in this case).

These test cases cover various scenarios to ensure the correct behavior of the lastIndexOf() method when searching for the last occurrence of a substring or character in a string. It's important to consider different edge cases and verify that the method returns the expected output in each scenario.