Java StringBuilder lastIndexOf() Method



The Java StringBuilder lastIndexOf() method is used, to retrieve the index of the rightmost occurrence of the given string in a StringBuilder object. The indices refer to the position of the character in the string.

The lastIndexOf() method accepts a parameter as a string that holds the value of the substring. It throws an exception if the given string is null. The method returns an integer value, if the substring does not found, it returns -1.

The lastIndexOf() method has two polymorphic variants with different parameters, such as − String, and fromIndex (below are the syntaxes of both polymorphic variants).

Syntax

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

public int lastIndexOf(String str)
public int lastIndexOf(String str, int fromIndex)

Parameters

  • str − This is the substring to search for.

  • fromIndex − This is the index to start the search from.

Return Value

This method returns the index of the first character of the last such substring is returned, if the string argument occurs one or more times as a substring within this object.If it does not occur as a substring, -1 is returned.

If you pass the fromIndex value as an argument along with the string, this method returns the index within this sequence of the last occurrence of the specified substring.

Example

If the given string value is not null, the lastIndexOf() method returns the index value of the substring.

In the following program, we are instantiating the StringBuilder with the value “Welcome to TutorialsPoint”. Using the lastIndexOf() method, we are trying to retrieve the index of the “to” substring.

package com.tutorialspoint.StringBuilder;
import java.lang.*;
public class Index {
   public static void main(String[] args) {
      
      //instantiating the StringBuilder
      StringBuilder sb = new StringBuilder("Welcome to TutorialsPoint");
      System.out.println("The given string is: " + sb);
      
      //initialize the substring and fromIndex value
      String str = "to";
      System.out.println("The sustring is: " + str);
      
      //using the indexOf() method
      System.out.println("The index value of '" + str + "' is: " + sb.lastIndexOf(str));
   }
} 

Output

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

The given string is: Welcome to TutorialsPoint
The sustring is: to
The index value of 'to' is: 13

Example

If the given string value is null, the lastIndexOf() method throws a NullPointerException.

In the following program, we are creating an object of the StringBuilder with the value “HelloWorld”. Using the lastIndexOf() method, we are trying to retrieve the index of the null value.

package com.tutorialspoint.StringBuilder;
import java.lang.*;
public class Index {
   public static void main(String[] args) {
      try {
         
         //instantiating the StringBuilder
         StringBuilder sb = new StringBuilder("HelloWorld");
         System.out.println("The given string is: " + sb);
         
         //initialize the substring and fromIndex value
         String str = null;
         int fromIndex = 5;
         System.out.println("The sustring is: " + str);
         System.out.println("The fromIndex value is: " + fromIndex);
         
         //using the indexOf() method
         System.out.println("The position of " + str + " is: " + sb.lastIndexOf(str, fromIndex));
      } catch(NullPointerException e) {
         e.printStackTrace();
         System.out.println("Exception: " + e);
      }
   }
}

Output

Following is the output of the above program −

The given string is: HelloWorld
The sustring is: null
The fromIndex value is: 5
java.lang.NullPointerException: Cannot read field "value" because "tgtStr" is null
	at java.base/java.lang.String.lastIndexOf(String.java:2632)
	at java.base/java.lang.AbstractStringBuilder.lastIndexOf(AbstractStringBuilder.java:1534)
	at java.base/java.lang.StringBuilder.lastIndexOf(StringBuilder.java:440)
	at com.tutorialspoint.StringBuilder.Index.main(Index.java:15)
Exception: java.lang.NullPointerException: Cannot read field "value" because "tgtStr" is null

Example

If the given string value is not null, but the fromIndex value is negative, this method returns -1.

In the following example, we are instantiating the StringBuilder with the value “Java Programming Language”. Then, using the lastIndexOf() method, we are trying to retrieve the index of the “Programming” substring from index -1.

package com.tutorialspoint.StringBuilder;
import java.lang.*;
public class Index {
   public static void main(String[] args) {
      
      //instantiating the StringBuilder
      StringBuilder sb = new StringBuilder("Java Programming Language");
      System.out.println("The given string is: " + sb);
      
      //initialize the substring and fromIndex value
      String str = "Programming";
      int fromIndex = -1;
      System.out.println("The sustring is: " + str);
      System.out.println("The fromIndex value is: " + fromIndex);
      
      //using the indexOf() method
      System.out.println("The position of " + str + " is: " + sb.lastIndexOf(str, fromIndex));
   }
}

Output

The above program, produces the following results −

The given string is: Java Programming Language
The sustring is: Programming
The fromIndex value is: -1
The position of Programming is: -1

Example

If the fromIndex value is positive, and greater than the substring index value, the lastIndexOf() method returns the index value of the substring.

In the following program, we are creating a StringBuilder with the value “TutorialsPoint”. Using the lastIndexof() method, we are trying to retrieve the index of the “als” substring from index 10.

package com.tutorialspoint.StringBuilder;
import java.lang.*;
public class Index {
   public static void main(String[] args) {
      
      //instantiating the StringBuilder
      StringBuilder sb = new StringBuilder("TutorialsPoint");
      System.out.println("The given string is: " + sb);
      
      //initialize the substring and fromIndex value
      String str = "als";
      int fromIndex = 10;
      System.out.println("The sustring is: " + str);
      System.out.println("The fromIndex value is: " + fromIndex);
      
      //using the indexOf() method
      System.out.println("The position of " + str + " is: " + sb.lastIndexOf(str, fromIndex));
   }
}

Output

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

The given string is: TutorialsPoint
The sustring is: als
The fromIndex value is: 10
The position of als is: 6
Advertisements