Java.lang.StringBuffer.lastIndexOf() Method
Advertisements
Description
The java.lang.StringBuffer.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.StringBuffer.lastIndexOf() method
public int lastIndexOf(String str)
Parameters
str -- This is the substring to search for.
Return Value
If the string argument occurs one or more times as a substring within this object, then the index of the first character of the last such substring is returned. 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.StringBuffer.lastIndexOf() method.
package com.tutorialspoint;
import java.lang.*;
public class StringBufferDemo {
public static void main(String[] args) {
StringBuffer buff = new StringBuffer("tutorials point");
System.out.println("buffer = " + buff);
/* returns the index within this string of the rightmost occurrence
of the specified substring */
System.out.println("last Index of a = " + buff.lastIndexOf("a"));
// returns -1 as substring am is not found
System.out.println("last index of am = " + buff.lastIndexOf("am"));
}
}
Let us compile and run the above program, this will produce the following result:
buffer = tutorials point last index of a = 6 last index of am = -1