Chryssa Aliferi

About Chryssa Aliferi

Chryssa is a Computer Science graduate from Athens University of Economic and Business. During her studies, Chryssa carried out a great variety of projects ranging from networking to software engineering. She is very keen on front end development especially on mobile technologies and web applications.

Java String lastIndexOf Example

As we disscused in a previous post about Java String indexOf() Example, many times we need to search for a character set in a Java String. Thus we need a “tool”, a method that will help us make it.

So, in this example we are going to talk about identifying characters and subsequences of characters through Java Strings, with the method lastIndexOf().

String static lastIndexOf() method:

The lastIndexOf()(String or character target) method searches right-to-left inside the given string for a “target” string. The method returns the index number where the target string is last found or -1 if the target is not found. Like equals(), the lastIndexOf() method is case-sensitive, so uppercase and lowercase chars are considered to be different. The count of the index number starts from left to right though.

  • int lastIndexOf(String str): Returns the index within this string of the last occurrence of the specified substring. If it does not occur as a substring, -1 is returned.
  • int lastIndexOf()(String str, int fromIndex): Returns the index within this string of the last occurrence of the specified character, searching backwards, starting at the specified index. If it does not occur, -1 is returned.

We can use the same methods in order to find the index of the last occurrence of a specified character within the string:

  • int lastIndexOf(int ch)
  • int lastIndexOf(int ch, int fromIndex)


package com.javacodegeeks.javabasics.string;

public class JavaStringLastIndexOf {

   public static void main(String args[]) {

   String str = new String("Hello JavaCodeGeeks! This is my second post.");

   String subStr1 = new String("JavaCodeGeeks");
   String subStr2 = new String("JavaGeeks");

   System.out.print("Found Index :");
   System.out.println(str.lastIndexOf('e')); //finds the last occurrence of 'e'

   System.out.print("Found Index :");
   System.out.println(str.lastIndexOf('e', 20)); //finds the last occurrence of 'e' before the 20th character

   System.out.print("Found Index :");
   System.out.println(str.lastIndexOf(subStr1)); //finds the last occurrence of "JavaCodeGeeks"

   System.out.print("Found Index :");
   System.out.println(str.lastIndexOf(subStr1, 15)); //finds the last occurrence of "JavaCodeGeeks" before the 15th character

   System.out.print("Found Index :");
   System.out.println(str.lastIndexOf(subStr2)); //finds the last occurrence of "JavaGeeks"



Found Index :33
Found Index :16
Found Index :6
Found Index :6
Found Index :-1

This was the example of Java String lastIndexOf() method.

Related Whitepaper:

Java Essential Training

Author David Gassner explores Java SE (Standard Edition), the language used to build mobile apps for Android devices, enterprise server applications, and more!

The course demonstrates how to install both Java and the Eclipse IDE and dives into the particulars of programming. The course also explains the fundamentals of Java, from creating simple variables, assigning values, and declaring methods to working with strings, arrays, and subclasses; reading and writing to text files; and implementing object oriented programming concepts. Exercise files are included with the course.

Get it Now!  

Examples Java Code Geeks and all content copyright © 2010-2014, Exelixis Media Ltd | Terms of Use | Privacy Policy
All trademarks and registered trademarks appearing on Examples Java Code Geeks are the property of their respective owners.
Java is a trademark or registered trademark of Oracle Corporation in the United States and other countries.
Examples Java Code Geeks is not connected to Oracle Corporation and is not sponsored by Oracle Corporation.

Sign up for our Newsletter

20,709 insiders are already enjoying weekly updates and complimentary whitepapers! Join them now to gain exclusive access to the latest news in the Java world, as well as insights about Android, Scala, Groovy and other related technologies.

As an extra bonus, by joining you will get our brand new e-books, published by Java Code Geeks and their JCG partners for your reading pleasure! Enter your info and stay on top of things,

  • Fresh trends
  • Cases and examples
  • Research and insights
  • Two complimentary e-books