Java - String lastIndexOf() Method



The Java String lastIndexOf() method is used to retrieve the position of the last occurrence of a specified character in the current string. The index refers to the character positions in a string.

The index ranges start from 0 and end with the length() -1. The first char value denotes the 0th index, the second char value denotes the 1st index, and so on. This method accept different parameters.

The lastIndexOf() method has four polymorphic variants with different parameters such as char, fromIndex, and string. Below are the syntaxes of all the polymorphic variants.

Syntax

Following is the syntax of the Java String lastIndexOf() method −

public int lastIndexOf(int ch) // first syntax 
public int lastIndexOf(int ch, int fromIndex)// second syntax
public int lastIndexOf(String str)// third syntax
public int lastIndexOf(String str, int fromIndex)// fourth syntax

Parameters

  • ch − This is the value of character(Unicode code point).

  • fromIndex − This is the starting position of the index.

  • str − This is value of string.

Return Value

  • This method returns the index within this string of the last occurrence of the specified character. // first

  • This method returns the index within this string of the last occurrence of the specified character, searching backward starting at the specified index. // second

  • This method returns the index within this string of the rightmost occurrence of the specified substring. // third

  • This method returns the index within this string of the last occurrence of the specified substring, searching backward starting at the specified index. // fourth

Example

If the given character value does not occur in the current string, the lastIndexOf() method returns -1.

In the following program, we are instantiating the string class with the value Hello. Using the lastIndexOf() method, we are trying to retrieve the index value of character p in the current string at the last occurrence.

import java.lang.*;
public class LastIndex {
   public static void main(String[] args) {
      
      //instantiate the string class
      String str = new String("Hello");
      System.out.println("The given string is: " + str);
      
      //initialize the char value
      char ch = 'p';
      System.out.println("The given char value is: " + ch);
      
      //using the lastIndexOf() method
      System.out.println("The position of the '" + ch + "' is: " + str.lastIndexOf(ch));
   }
}

Output

On executing the above program, it will produce the following result −

The given string is: Hello
The given char value is: p
The position of the 'p' is: -1

Example

If we pass char and fromIndex as an argument to the method, this method returns the index within this string of the last occurrence of the specified character.

In the following example, we create an object of the string class with the value HelloWorld. Then, using the lastIndexOf() method, we are trying to retrieve the character o position in the string at the specified fromIndex 5.

import java.lang.*;
public class LastIndex {
   public static void main(String[] args) {
      
      //create an object of the string class
      String str = new String("HelloWorld");
      System.out.println("The given string is: " + str);
      
      //initialize the char value and fromIndex value
      char ch = 'o';
      int fromIndex = 5;
      System.out.println("The given char and fromIndex values are: " + ch + " and " + fromIndex);
      
      //using the lastIndexOf() method
      System.out.println("The position of the '" + ch + "' is: " + str.lastIndexOf(ch, fromIndex));
   }
}

Output

Following is the output of the above program −

The given string is: HelloWorld
The given char and fromIndex values are: o and 5
The position of the 'o' is: 4

Example

If we pass the string as an argument to the method, this method returns the string position in the current string.

In this program, we are creating a string literal with the value TutorialsPoint. Using the lastIndexOf() method, we are trying to retrieve the position of the specified string als in the given string value at the last occurrence.

import java.lang.*;
public class LastIndex {
   public static void main(String[] args) {
      
      //create string literal
      String str = "TutorialsPoint";
      System.out.println("The given string is: " + str);
      
      //initialize argument string
      String str_argu = "als";
      System.out.println("The argument string is: " + str_argu);
      
      //using the lastIndexOf() method
      System.out.println("The position of the '" + str_argu + "' is: " + str.lastIndexOf(str_argu));
   }
}

Output

The above program, produces the following output −

The given string is: TutorialsPoint
The argument string is: als
The position of the 'als' is: 6

Example

If we pass the string and fromIndex as arguments to the method, this method returns the index within this string of the last occurrence.

In the following program, we are instantiating the string class with the value Java Programming Language. Using the lastIndexOf() method, we are trying to retrieve the string argument position in the current string at the specified fromIndex 10.

import java.lang.*;
public class LastIndex {
   public static void main(String[] args) {
      
      //instantiate the  string class
      String str = new String("Java Programming Language");
      System.out.println("The given string is: " + str);
      
      //initialize argument string and fromIndex value
      String str_argu = "Programming";
      int fromIndex = 10;
      System.out.println("The given argument string and fromIndex values are: " + str_argu + " and " + fromIndex);
      
      //using the lastIndexOf() method
      System.out.println("The position of the '" + str_argu + "' is: " + str.lastIndexOf(str_argu, fromIndex));
   }
}

Output

After executing the above program, it will produce the following output −

The given string is: Java Programming Language
The given argument string and fromIndex values are: Programming and 10
The position of the 'Programming' is: 5
java_lang_string.htm
Advertisements