The String lastIndexOf() method accepts a String as the argument, and if the string argument occurs one or more times as the substring within this object, it returns an index of the first character of the last such substring is returned.
Java String lastIndexOf()
Java String lastIndexOf() is a built-in method used to find out the index of the last occurrence of a character or the starting index of the last occurrence of a substring in a given String.
The Java lastIndexOf() method comes in four variations:
- public int lastIndexOf(int ch)
- public int lastIndexOf(int ch, int end)
- public int lastIndexOf(String sub)
- public int lastIndexOf(String sub, int end)
Figure
Snippet
public int lastIndexOf(int ch){ return lastIndexOf(ch, value.length-1); }
Finding the last index of occurrence of a character
Syntax: public int lastIndexOf(int ch)
Parameters: The character whose last index of occurrence is to be found
Returns: An integer, which is the last index of occurrence required. Returns -1 if not found.
See the following code.
public class Example1 { public static void main(String[] args) { String s1 = new String("Consider this example."); System.out.println(s1); System.out.println(s1.lastIndexOf('e')); } }
Output
Example2.java:
See the following code.
public class Example2 { public static void main(String[] args) { String s1 = new String("Consider this example."); System.out.println(s1); System.out.println(s1.lastIndexOf('z')); } }
Output
Finding the last index of occurrence of a character up to a particular index
Syntax:
public int lastIndexOf(int ch, int end)
Parameters:
int ch: The character whose last index of occurrence is to be found.
int end: The index up to which the search is to be done.
Returns: An integer, the last index of occurrence required up to the specified index. Returns -1 if not found.
See the following code example.
public class Example3 { public static void main(String[] args) { String s1 = new String("Consider this example."); System.out.println(s1); System.out.println(s1.lastIndexOf('e', 19)); } }
Output
See another example.
public class Example3a { public static void main(String[] args) { String s1 = new String("Consider this example."); System.out.println(s1); System.out.println(s1.lastIndexOf('e', 20)); } }
Output
See the third example.
public class Example3b { public static void main(String[] args) { String s1 = new String("Consider this example."); System.out.println(s1); System.out.println(s1.lastIndexOf('e', 3)); } }
Output
Finding a starting index of the last occurrence of a substring
Syntax: public int lastIndexOf(String sub)
Parameters: The substring whose starting index of the last occurrence is found.
Returns: An integer, which is the last index of occurrence required. Returns -1 if not found.
See the following code.
public class Example4 { public static void main(String[] args) { String s1 = new String("Consider this example. Now consider this."); System.out.println(s1); System.out.println(s1.lastIndexOf("this")); } }
Output
Finding the starting index of the last occurrence of a substring up to a particular index
Syntax: public int lastIndexOf(String sub, int end)
Parameters:
String sub: The substring whose starting index of the last occurrence is found.
int end: The index up to which the search is to be done.
Returns: An integer, the last index of occurrence required up to the specified index. Returns -1 if not found.
Example5.java
public class Example5 { public static void main(String[] args) { String s1 = new String("Consider this example. Now consider this."); System.out.println(s1); System.out.println(s1.lastIndexOf("this", 20)); } }
Output
Conclusion
So to find out the last occurrence of the specified character or a string, Java string lastIndexOf() method starts the search from the end of the string and proceeds backward from there.
If the fromIndex is specified during the method call, then the backward search begins from the specified index fromIndex.
Finally, Java String lastIndexOf() Function Example is over.