Yandex

Java Advanced ConceptsJava Advanced Concepts3

Java String lastIndexOf() method
Syntax and Examples



The lastIndexOf() method in Java is a handy tool for finding the last occurrence of a character or substring within a string. It's like searching backward through your text to see where something appears most recently.

Syntax

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

Parameters

ParameterDescription
chThe character to search for. It is treated as an int representing the Unicode value of the character.
ch, fromIndexThe character to search for and the index from which to start the backward search. The search starts at fromIndex - 1 and goes backwards towards the beginning of the string.
strThe string to search for.
str, fromIndexThe string to search for and the index from which to start the backward search. The search starts at fromIndex - 1 and goes backwards towards the beginning of the string.

Return Value

The lastIndexOf() method returns the index of the last occurrence of the specified character or substring within the string. If the character or substring is not found, it returns -1.

Examples

Example 1: Finding the Last Occurrence of a Character

This example demonstrates how to find the last index of a specific character in a string.

String str = "Hello, World!";
int index = str.lastIndexOf('l');
System.out.println(index);
9

Explanation: The character 'l' appears three times in the string "Hello, World!". The lastIndexOf() method finds the last occurrence of 'l', which is at index 9.

Example 2: Finding the Last Occurrence with a Starting Index

This example shows how to restrict the search for a character to start from a specific index, searching backward from that point.

String str = "Hello, World!";
int index = str.lastIndexOf('l', 5);
System.out.println(index);
2

Explanation: We're searching for the last 'l' but only within the string from index 5 backwards. Therefore, it finds the 'l' at index 2.

Example 3: Finding the Last Occurrence of a Substring

This example illustrates how to find the last index of a substring in a string.

String str = "Java is fun, Java is powerful";
int index = str.lastIndexOf("Java");
System.out.println(index);
18

Explanation: The substring "Java" appears twice in the string. The lastIndexOf() method returns the index of the last occurrence, which is at position 18.

Example 4: Finding Last Occurrence of a Substring with Starting Index

This example demonstrates searching for a substring backward from a given index.

String str = "Java is fun, Java is powerful";
int index = str.lastIndexOf("Java", 15);
System.out.println(index);
0

Explanation: The substring 'Java' appears twice in the string, but we limit our search to indexes before 15 (i.e., from index 14 backwards). Therefore, only the first occurrence at index 0 is found.



Welcome to ProgramGuru

Sign up to start your journey with us

Support ProgramGuru.org

You can support this website with a contribution of your choice.

When making a contribution, mention your name, and programguru.org in the message. Your name shall be displayed in the sponsors list.

PayPal

UPI

PhonePe QR

MALLIKARJUNA M