filmov
tv
How to find the index position of a substring or character in a String with lastIndexOf() method

Показать описание
How to find out the index position of a given substring or character in a given String using the Java lastIndexOf() method
In this video, we will talk about the Java lastIndexOf() method, which we can use to find out the index of the last occurrence of a substring or a character in a given String.
What happens when this method is evaluated is that, it starts to search for the occurrence of the substring or character from the end of the String and proceeds backwards from there. Just like the indexOf() method, there are two variations of this method in the String class
1. In the first example, I will show you how to return the index position of the last occurrence of a character or a substring in a given String
String str = “I love programming”;
Since the method returns an integer value, I will declare an integer variable to store the value returned whenever the method is called and executed
2. In the second example, I will show you how to return the index of the last occurrence of a character or substring in a given String While specifying an index position. The method will start searching the given backwards starting from the specified index
String str = “I love programming”;
Since the method returns an integer value, I will declare an integer variable to store the value returned whenever the method is called and executed
10 here represents the specified index position from where the method will start looking for the character o.
Also, the method will return -1 if the character or the substring is not found in the given String. That’s why we get -1 in the second statement.
#codingriver
#java
#programming
In this video, we will talk about the Java lastIndexOf() method, which we can use to find out the index of the last occurrence of a substring or a character in a given String.
What happens when this method is evaluated is that, it starts to search for the occurrence of the substring or character from the end of the String and proceeds backwards from there. Just like the indexOf() method, there are two variations of this method in the String class
1. In the first example, I will show you how to return the index position of the last occurrence of a character or a substring in a given String
String str = “I love programming”;
Since the method returns an integer value, I will declare an integer variable to store the value returned whenever the method is called and executed
2. In the second example, I will show you how to return the index of the last occurrence of a character or substring in a given String While specifying an index position. The method will start searching the given backwards starting from the specified index
String str = “I love programming”;
Since the method returns an integer value, I will declare an integer variable to store the value returned whenever the method is called and executed
10 here represents the specified index position from where the method will start looking for the character o.
Also, the method will return -1 if the character or the substring is not found in the given String. That’s why we get -1 in the second statement.
#codingriver
#java
#programming