To find out the last occurrence of a personality or string from a given string across which the substring or character is been searched over and returning the index value of the character or substring is found.
Consider an example of a book. The book contains pages where pages contain words. Now if a user wants to figure out the occurrence of a word of his own choice then the reader’s head rebound of question does that word to be searched even exist in the book And if yes, starts flipping out the pages prior to remembering the page where it last occurred because there may be chances that the word would repeat after the page which he updated the last and is done reading the book.
Here the set of all the words in the book is referred to as ‘String’ and the word to be searched is referred to as ‘word’ itself. The exact place where the word occurred is referred to as ‘index’.
Now in Java,
there's already an inbuilt method defined in strings over
handling indexes in it
referred to as indexOf().
it's utilized in checking out the index of a special character or sub-string itself from a given string. As usual, there are two possibilities, first is when
a personality or substring is present
within the string it returns the index, and if not this function returns ‘-1’ if the character or substring
isn't present
call at the string. -1 is returned in j
ava as in java
there's no such concept of negative indexed
what's present therein python.
Similarly, there's also a predefined inbuilt function defined referred to as lastIndexOf() which specifies the last occurrence of character or substring present within the string. the tactic starts performing from the last of the string and moves backwards. Now if the ‘fromIndex is specified’ during the call of lastIndexOf() method, then look for character or substring starts from the before ‘frontIndex’ rather than last of string.
- int lastIndexOf(int ch) // Returning the last index of occurrence of character in string.
- int lastIndexOf(int ch, int fromIndex) // Returning the last index looking backwards ranging from ‘frontIndex’.
- int latIndexOf(String str) // Returning the last index of substring named ‘str’ from last of string.
- int lastIndexOf(String str, int fromIndex) // Returning the last occurrence of substring ranging from frontIndex in string.
The directory in which inbuilt function is present:
java.util.String.lastIndexOf()
Return type:
- Positive Numeric value: Value at which the last occurrence of a character or substring in a string is matched
- -1 If not matched
Approach:
- Create a category with the name.
- Declare a variable with a reputation string and initialize the variable with value as “geeksforgeeks”
- Now declare another variable with name word and initialize the variable with value as “geeks”. This word is that the search word during this scenario.
- After initializing with the worth now we will use the lastIndexOf() method and will pass the string and word as an argument.
- After following the above steps then compile the program.
- Once the program is compiled you'll get the output.
Example 1:
Considering where the character or substring isn't present within the string then it'll return the last index value.
// Java Program to find the Last
// Index of a particular word
// in a string
// Importing Classes/Files
import java.util.*;
public class GFG {
// Main driver method
public static void main(String[] args)
{
// String in which char or
// substring to be searched
String str = "geeksforgeeks";
// Substring to be searched
String word = "geeks";
// Printing last index of char
// or substring is found
// Printing -1 is not found
System.out.println(str.lastIndexOf(word));
}
}
Output
8
Example 2: Considering where the character or substring is not present in the string then it will return a ‘-1’ value.
// Java Program to find the Last
// Index of a particular word
// in a string
// Importing Classes/Files
import java.util.*;
public class GFG {
// Main driver method
public static void main(String[] args)
{
// String in which char or
// substring to be searched
String str = "geeksforgeek";
// Substring to be searched
String word = "gfg";
// Printing last index of char
// or substring is found
// Printing -1 is not found
System.out.println(str.lastIndexOf(word));
}
}
Output
-1