⬅ Previous Topic
Java Stringjoin()
methodNext Topic ⮕
Java Stringlength()
methodlastIndexOf()
method⬅ Previous Topic
Java Stringjoin()
methodNext Topic ⮕
Java Stringlength()
methodThe 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.
public int lastIndexOf(int ch)
public int lastIndexOf(int ch, int fromIndex)
public int lastIndexOf(String str)
public int lastIndexOf(String str, int fromIndex)
Parameter | Description |
---|---|
ch | The character to search for. It is treated as an int representing the Unicode value of the character. |
ch , fromIndex | The 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. |
str | The string to search for. |
str , fromIndex | The 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. |
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.
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.
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.
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.
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.
⬅ Previous Topic
Java Stringjoin()
methodNext Topic ⮕
Java Stringlength()
methodYou 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.