Java String lastIndexOf()The Java String class lastIndexOf() method returns the last index of the given character value or substring. If it is not found, it returns -1. The index counter starts from zero. SignatureThere are four types of lastIndexOf() method in Java. The signature of the methods are given below:
Parametersch: char value i.e. a single character e.g. 'a' fromIndex: index position from where index of the char value or substring is retured substring: substring to be searched in this string Returnslast index of the string Internal ImplementationThe internal implementation of the four types of the lastIndexOf() method is mentioned below. The internal implementation of the four types of the lastIndexOf() method is mentioned below. 1. int lastIndexOf(int ch) 2. int lastIndexOf(int ch, int fromIndex) 3. int lastIndexOf(String subString) 4. int lastIndexOf(String substring, int fromIndex) Java String lastIndexOf() method exampleFileName: LastIndexOfExample.java Test it NowOutput: 6 Java String lastIndexOf(int ch, int fromIndex) Method ExampleHere, we are finding the last index from the string by specifying fromIndex. FileName: LastIndexOfExample2.java Test it NowOutput: 3 Java String lastIndexOf(String substring) Method ExampleIt returns the last index of the substring. FileName: LastIndexOfExample3.java Test it NowOutput: 19 Java String lastIndexOf(String substring, int fromIndex) Method ExampleIt returns the last index of the substring from the fromIndex. FileName: LastIndexOfExample4.java Test it NowOutput: 19 -1 Next TopicJava String length() |
We provides tutorials and interview questions of all technology like java tutorial, android, java frameworks
G-13, 2nd Floor, Sec-3, Noida, UP, 201301, India