Java.lang.StringBuilder.lastIndexOf() Method

Advertisements


Description

The java.lang.StringBuilder.lastIndexOf(String str) method returns the index within this string of the rightmost occurrence of the specified substring.

Declaration

Following is the declaration for java.lang.StringBuilder.lastIndexOf() method

public int lastIndexOf(String str)

Parameters

  • str -- This is the substring to search for.

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.

Exception

  • NullPointerException -- if str is null.

Example

The following example shows the usage of java.lang.StringBuilder.lastIndexOf() method.

package com.tutorialspoint;

import java.lang.*;

public class StringBuilderDemo {

  public static void main(String[] args) {
  
    StringBuilder str = new StringBuilder("tutorials point");
    System.out.println("string = " + str);
    
    /* returns the index within this string of the rightmost occurrence 
    of the specified substring */
    System.out.println("last Index of a = " + str.lastIndexOf("a"));
  
    // returns -1 as substring am is not found
    System.out.println("last index of am = " + str.lastIndexOf("am"));
  }
}  

Let us compile and run the above program, this will produce the following result:

string = tutorials point
last Index of a = 6
last index of am = -1



Advertisements
Advertisements