×

Java String lastIndexOf() method

Java String lastIndexOf() method returns last index of character or substring in a String.

Syntax:

Method Description

int lastIndexOf(int ch)It returns last index position for the given char value
int lastIndexOf(int ch, int fromIndex)It returns last index position for the given char value and from index
int lastIndexOf(String substring)It returns last index position for the given substring
int lastIndexOf(String substring, int fromIndex)It returns last index position for the given substring and from index

Parameter

ch : a single character

fromIndex : index position from where index of the char value or substring is returned

substring : substring to be searched in current String

Returns

It returns last index of the given String

Java String lastIndexOf() method example

          public class JavaStringLastIndexOfEx1
{ 
                  public static void main(String args[])
                { 
    String s1="Java String Last IndexOf method Examples";//there are 2 's' characters in this sentence 
    int index1=s1.lastIndexOf('s');//returns last index of 's' char value 
    int index2=s1.indexOf('s');
    System.out.println("Last index  : "+index1);//39
    System.out.println("Index :"+index2);//14
      }      
    }

Output:

Last index  : 39
Index :14

Java String lastIndexOf(int ch, int fromIndex) method example

    public class JavaStringLastIndexOfEx2 { 
        public static void main(String[] args) { 
            String s1="Java String Last IndexOf method Examples";
            int index = s1.lastIndexOf('a',7); 
            System.out.println(index);       
        } 
    }

Output:

3

Java String lastIndexOf(String substring) method example

public class JavaStringLastIndexOfEx3
 { 
        public static void main(String[] args)
        {          
            String str = "Java String Last IndexOf method Examples"; 
            int index = str.lastIndexOf("method"); 
            System.out.println(index);       
        } 
    }

Output:

25

Java String lastIndexOf(String substring, int fromIndex) method example

    public class JavaStringLastIndexOfEx4
    { 
        public static void main(String[] args)
        {          
            String str = "Java String Last IndexOf method Examples"; 
            int index = str.lastIndexOf("Of", 25); 
            System.out.println(index); 
            index = str.lastIndexOf("Of", 10); 
            System.out.println(index); // -1, if not found       
        } 
    }

Output:

22
-1

Related Topics

Java Primitive Data Types

Primitive data types are the simplest data types in a programming language. They’re predefined in the language. The names of the primitive types are quite descriptive of the values that...

2 minutes read.

Program to Find the Common Elements between two Arrays in Java

In this article, we are going to learn how to find the common elements between two arrays using Java. Here, we use different approaches in Java to find the common...

3 minutes read.

Add numbers represented by Linked Lists in Java

For calculating the sum of the two numbers that are represented by a linked list, and then store the result in a new linked list. A linked list's head node...

7 minutes read.

Convert List to String in Java

We occasionally need to create a string from a list of characters. Since a string is only a collection of characters, a character array can be converted into a string....

6 minutes read.

Java Characters

Normally, when we work with characters, we use primitive data types char. When we have to work with the objects of char, we use Character class. Character class has many important...

2 minutes read.

User Defined Exception in Java

An exception is an error (run time error) that happened while a program was being executed. The program stops abruptly whenever the Exception occurs, and the code after the line...

3 minutes read.

Intersection Point of two linked list in Java

In this article, you will be very well acknowledged about how to achieve the intersection point of two linked list in Java. There are several approaches to obtain. Surely each...

8 minutes read.

Java Protected Keyword

An access modifier is a keyword that Java protects. It can be used to constructors, methods, inner classes, and variables. Variables, methods, and constructors that have been marked protected in...

3 minutes read.

Creation of Multi Thread in java

What are threads in Java? We can use threads to facilitate parallel processing. Threads are helpful when you wish to execute several pieces of code concurrently. A thread is a small process...

3 minutes read.

Java Location

PATH is an environmental variable that the system software now uses to find executable files (.exe) or Java binaries ( java or javac command). Once chosen, a route cannot be...

3 minutes read.

Convert JSON to Map in Java

JACKSON and GSON libraries are two extremely capable JSON-related libraries offered by Java. To efficiently work with the returned JSON data, we frequently need to convert JSON responses into a...

6 minutes read.

Difference Between replace() and replaceall() in Java

In this article, you will be acknowledged about the replace() and replaceall() methods in java. Also, most importantly you will be acknowledged the differences between them along with the example...

4 minutes read.

Java Switch string

A multi-way branch statement is the switch statement. It offers a simple method for allocating execution to various code sections according to the expression's value. Primitive data types, including bytes,...

3 minutes read.

How to check data type in Java

In this section, we will learn about the methodology to verify the data type in Java. There are mainly two methods in java called as getClass() and getName(). They are...

3 minutes read.

Java Extends vs Implements

Java: We known that the java is a pure object oriented programming language. Java programming language consists of many features such as portable, plat form independence, secured, robust, simple, architecture neutral,...

4 minutes read.

Rotate matrix by 90 degrees in Java | Rotate matrix in Java clockwise and anti-clockwise

In this article, you will be acknowledged about what is a matrix along with an example. Most importantly you will be equipped knowledge on how to rotate the matrix by...

4 minutes read.

Multithreading in Java

Multithreading is a specialized form of multitasking. It is responsible for executing more than one task at a time of a single program, and each task is a separate thread. A program...

8 minutes read.

Java Buffer Reader

When a variable cannot change its value during run time is called a Static way of programming. In this programming, a variable is directly assigned to a value. Program: Import java.io.*; class  A {  ...

4 minutes read.

Modules in Golang

Modules are a way to manage dependency versions and enable reproducible builds of Go programs. They were introduced in Go 1.11 and are now the recommended way to manage dependencies...

4 minutes read.

Sort Dates in Java

The Collection class's sort() method, which is a component of the Comparator mechanism, arranges the data in decreasing order. The Comparator interface can be used to accomplish the goal while...

4 minutes read.