Java String lastIndexOf() Function: Complete Guide

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:

  1. public int lastIndexOf(int ch)
  2. public int lastIndexOf(int ch, int end)
  3. public int lastIndexOf(String sub)
  4. public int lastIndexOf(String sub, int end)

Figure

Java String lastIndexOf()

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

Java String lastIndexOf() Example

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

Java String lastIndex

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

Finding the last index of occurrence of a character up to a certain index

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

Java String example

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

Java string lastIndexOf() tutorial

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 a starting index of the last occurrence of a substring

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

Example5

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.

Related Posts

Java String startsWith()

Java String split()

Java String join()

Java String intern()

Java String replace()

Leave a Comment

This site uses Akismet to reduce spam. Learn how your comment data is processed.