AppDividend
Latest Code Tutorials

Java String lastIndexOf() Function Example

0

Java String lastIndexOf() is an inbuilt method that is 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. This 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)

Java String lastIndexOf()

This method accepts a String as the argument, and if the string argument occurs one or more times as the substring within this object, then it returns an index of the first character of the last such substring is returned.

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, which is 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 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 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 certain index

Syntax: public int lastIndexOf(String sub, int end)

Parameters: 

String sub: The substring whose starting index of the last occurrence is to be found.

int end: The index up to which the search is to be done.

Returns: An integer, which is 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 Reply

Your email address will not be published.

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