Java String lastIndexOf() method example

The Java String lastIndexOf() method returns the last index of specified character or string as method argument. If argument is not found in string, method returns -1. The index counter for a string starts from zero.

Java String lastIndexOf() method syntax

String lastIndexOf() method has four overloaded forms:

No. Method syntax Description
1. int lastIndexOf(String substring) returns last index position for the given substring
2. int lastIndexOf(String substring, int fromIndex) returns last index position for the given substring searching backward starting at the specified fromIndex
3. int lastIndexOf(int ch) returns last index position for the given char value
4. int lastIndexOf(int ch, int fromIndex) returns index position for the given char value searching backward starting at the specified fromIndex

‘null’ argument is not allowed

Passing null argument is not allowed to lastIndexOf() method. It will result in NullPointerException exception.

String blogName = "howtodoinjava.com";
System.out.println( blogName.lastIndexOf(null) );

//Program output

Exception in thread "main" java.lang.NullPointerException
	at java.lang.String.lastIndexOf(String.java:1705)
	at java.lang.String.lastIndexOf(String.java:1685)
	at com.StringExample.main(StringExample.java:9)

1. Java String lastIndexOf(String substring) example

Java program to find last index of substring in a given a string object using lastIndexOf(String substring) method.

public class StringExample 
{
    public static void main(String[] args) 
    {
        String blogName = "howtodoinjava.com";
        
        System.out.println( blogName.lastIndexOf("java") );			//9
        
        System.out.println( "hello world".lastIndexOf("world") );	//6
        
        System.out.println( "hello world".lastIndexOf("earth") );	//-1
    }
}

Program output.

9
6
-1

2. Java String lastIndexOf(String substring, int fromIndex) example

Java program to find last index of substring in a given a string object, searching backward starting at the specified fromIndex using indexOf(String substring, int fromIndex) method.

Please note that when a substring is found, index counting starts with 0 index and from beginning of string only.

public class StringExample 
{
    public static void main(String[] args) 
    {
        String blogName = "howtodoinjava.com";
        
        System.out.println( blogName.indexOf("java", 5) );			//9
        System.out.println( "hello world".indexOf("world", 6) );	//6
        System.out.println( "hello world".indexOf("world", 2) );	//6
        System.out.println( "hello world".indexOf("world", 10) );	//-1
    }
}

Program output.

9
6
6
-1

3. Java String lastIndexOf(char ch) example

Java program to find last index of given character 'ch' in a given a string object using lastIndexOf(char ch) method.

public class StringExample 
{
    public static void main(String[] args) 
    {
        String blogName = "howtodoinjava.com";
        
        System.out.println( blogName.lastIndexOf('j') );            //9
        System.out.println( "hello world".lastIndexOf('w') );       //6
        System.out.println( "hello world".lastIndexOf('k') );       //-1
    }
}

Program output.

9
6
-1

4. Java String lastIndexOf(int ch, int fromIndex) example

Java program to find last index of character 'ch' in a given a string object, searching backward starting at the specified fromIndex using lastIndexOf(String substring, int fromIndex) method.

Please note that when the character is found, index counting starts with 0 index and from beginning of string only.

public class StringExample 
{
    public static void main(String[] args) 
    {
        String blogName = "howtodoinjava.com";
        
        System.out.println( blogName.lastIndexOf('j', 4) );         //9
        System.out.println( "hello world".lastIndexOf('w', 2) );    //6
        System.out.println( "hello world".lastIndexOf('w', 6) );    //6
        System.out.println( "hello world".lastIndexOf('k') );       //-1
    }
}

Program output.

9
6
6
-1

Happy Learning !!

Reference:

Java String doc

Leave a Reply

0 Comments
Inline Feedbacks
View all comments

About Us

HowToDoInJava provides tutorials and how-to guides on Java and related technologies.

It also shares the best practices, algorithms & solutions and frequently asked interview questions.

Our Blogs

REST API Tutorial