In this guide, you will learn about the String lastIndexOf() method in Java programming and how to use it with an example.
1. String lastIndexOf() Method Overview
Definition:
The lastIndexOf() method in Java's String class returns the index of the last occurrence of the specified character or substring within the invoking string. If the character or substring doesn't exist, -1 is returned.
Syntax:
1. str.lastIndexOf(int ch)
2. str.lastIndexOf(int ch, int fromIndex)
3. str.lastIndexOf(String substring)
4. str.lastIndexOf(String substring, int fromIndex)
Parameters:
- ch: a character (Unicode code point).
- fromIndex: the index to start the search backward from.
- substring: a string to be searched.
Key Points:
- Returns the index of the last occurrence of the specified character or substring.
- Returns -1 if the character or substring is not found.
- The search begins at the specified fromIndex and moves backward, searching towards the beginning of this string.
- If the fromIndex argument is greater than or equal to the string's length, it starts the search at the end of the string.
2. String lastIndexOf() Method Example
public class LastIndexOfExample {
public static void main(String[] args) {
String sample = "JavaProgrammingJava";
// Searching for a character's last occurrence
int lastIndex1 = sample.lastIndexOf('a');
System.out.println("Last occurrence of 'a': " + lastIndex1);
// Searching backward from a specific index
int lastIndex2 = sample.lastIndexOf('a', 10);
System.out.println("Last occurrence of 'a' before index 10: " + lastIndex2);
// Searching for a substring's last occurrence
int lastIndex3 = sample.lastIndexOf("Java");
System.out.println("Last occurrence of 'Java': " + lastIndex3);
// Searching for a non-existent substring
int lastIndex4 = sample.lastIndexOf("Hello");
System.out.println("Searching for 'Hello': " + lastIndex4);
}
}
Output:
Last occurrence of 'a': 17 Last occurrence of 'a' before index 10: 3 Last occurrence of 'Java': 15 Searching for 'Hello': -1
Explanation:
In the example:
1. We start by searching for the last occurrence of the character 'a' in the string "JavaProgrammingJava". It returns the index of the last occurrence.
2. Next, we search backward from index 10 to find the character 'a'. The method finds the last occurrence before that index.
3. Then, we search for the last occurrence of the substring "Java". The method provides the starting index of its last occurrence.
4. Lastly, we search for a non-existent substring "Hello". As it's not present in the string, -1 is returned.
Related Java String Class method examples
- Java String charAt() example
- Java String concat() example
- Java String contains() example
- Java String endsWith() example
- Java String equals() example
- Java String equalsIgnoreCase() example
- Java String getBytes() example
- Java String indexOf() example
- Java String isEmpty() example
- Java String lastIndexOf() example
- Java String length() example
- Java String replace() example
- Java String split() example
- Java String startsWith() example
- Java String substring() example
- Java String toLowerCase() example
- Java String toUpperCase() example
- Java String trim() example
- Java String valueOf() example
Comments
Post a Comment
Leave Comment