×

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

Methods in Java

The Methods in Java are the collection of statements that are executed when the method is called. By using the methods, the complexity of writing the code decreases. The method consists...

4 minutes read.

What is interpreter in Java?

The programming language Java is platform-neutral. Therefore, can use Java on any platform that supports the Java processor. The Piece of software transforms the Java bytecode contained in the class...

5 minutes read.

Sorting Algorithms in Java

Sorting Algorithms in Java Sorting is the technique that puts the elements of an array or list either in descending or ascending order. For example, take an array A, whose elements...

3 minutes read.

Morris Traversal for Preorder in Java

Without the use of recursion or stacks, we traverse a tree using the Morris algorithm. The linked binary tree is the foundation of the Morris traversal. Preorder Morris Traversal Algorithm The preorder...

3 minutes read.

Bad Operand types for Binary Operator Java

We will discuss how to handle issues in bad operand types for binary operator &, bad operand types for binary operator &&, bad operand types for binary operator ==, and...

4 minutes read.

Java Virtual Machine (JVM)

JVM is a virtual runtime environment to execute Java byte codes. The JVM doesn’t understand the keywords we used to write code. That is why it is converted into bytecode. It controls the...

3 minutes read.

Java ResultSetMetaData

The data about another data is called Metadata. The ResultSetMetaData is used to store the data about ResultSet. The ResultSet Contains the columns, rows, names of table, datatypes etc. these...

2 minutes read.

Java program to count the occurrences of each character

The count of each character is the frequency of the characters. For example, the given String is “Programming”. In the given string, the count of the characters ‘P’ -1, ’r’-2,...

6 minutes read.

Java String Inbuilt functions

There are different types of inbuilt functions available in the Java String class, all of them are listed below. Char chat At (int index)It outputs the character indicated by the index....

5 minutes read.

Stack Program in Java

Stack Program in Java The Stack class is part of the collection framework that inherits the Vector class. Thus, the Stack class can also be called a subclass of the Vector...

5 minutes read.

Java finalize()

Java finalize() In Java, the Object class is the root class that is inherited by all the Java classes. The class provides the finalize() method that is called just before the...

4 minutes read.

Java Throw and Throws Keyword

Exceptions in Java enable us to construct high-quality programs where faults are checked at compile time rather than run time and where we may define unique exceptions that make code...

6 minutes read.

Local Minima in Java

An Array Finding a local minimum in an array a[0. m-1] of different integers is the job. A[i] is considered a local minimum if it is smaller than two of its...

4 minutes read.

Java Wrapper classes

Java Wrapper classes A wrapper class is a class whose object contains a primitive data type; moreover it provides a way to use primitive data type (int, boolean, etc.) as objects. Wrapper...

2 minutes read.

Three-way operator in Java

The ternary operator in Java is the only conditional operator that takes three operands. It's a popular one-line substitute for the if-then-else expression among Java programmers. If-else clauses can be...

3 minutes read.

Java String substring() method

Java String substring() method returns a part of the String. Syntax: public String substring(int startIndex) public String substring(int startIndex, int endIndex) Parameters: startIndex : starting index endIndex : ending index Returns: It returns a specified String. Throws: StringIndexOutOfBoundsException if start...

2 minutes read.

Java Get File Size

There are three ways to get the file size in Java. Using Java InputOutput Using NewInputOutput Library Files.size() Method FileChannel.size() Method Using Apache Commons InputOutput Using Java InputOutput: The Java IO package offers the classes that deal...

5 minutes read.

Java Math expm1() Method

The expm1() method of Math class returns ex-1 where e represents Euler’s number. Syntax: public static double expm1(double x) Parameters: The parameter ‘x’ represents the exponent to raise e in the calculation of ex-1. Return...

2 minutes read.

Callable Statement in Java

The Callable statement in Java is used to call the functions and Stored procedures. Example: If we want to know about the age of a person based on their date of birth,...

3 minutes read.

Java vs JavaScript

Java Vs JavaScript Java and JavaScript, both play an important role in the field of Computer Technology. Most people think JavaScript is a part of Java. But it is not entirely...

4 minutes read.