- Java.lang Package classes
- Java.lang - Home
- Java.lang - Boolean
- Java.lang - Byte
- Java.lang - Character
- Java.lang - Character.Subset
- Java.lang - Character.UnicodeBlock
- Java.lang - Class
- Java.lang - ClassLoader
- Java.lang - Compiler
- Java.lang - Double
- Java.lang - Enum
- Java.lang - Float
- Java.lang - InheritableThreadLocal
- Java.lang - Integer
- Java.lang - Long
- Java.lang - Math
- Java.lang - Number
- Java.lang - Object
- Java.lang - Package
- Java.lang - Process
- Java.lang - ProcessBuilder
- Java.lang - Runtime
- Java.lang - RuntimePermission
- Java.lang - SecurityManager
- Java.lang - Short
- Java.lang - StackTraceElement
- Java.lang - StrictMath
- Java.lang - String
- Java.lang - StringBuffer
- Java.lang - StringBuilder
- Java.lang - System
- Java.lang - Thread
- Java.lang - ThreadGroup
- Java.lang - ThreadLocal
- Java.lang - Throwable
- Java.lang - Void
- Java.lang Package extras
- Java.lang - Interfaces
- Java.lang - Errors
- Java.lang - Exceptions
- Java.lang Package Useful Resources
- Java.lang - Useful Resources
- Java.lang - Discussion
Java StringBuffer lastIndexOf() Method
The Java StringBuffer lastIndexOf() method is used, to retrieve the index of the rightmost occurrence of the given string in a StringBuffer 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 StringBuffer 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 StringBuffer with the value “Welcome to TutorialsPoint”. Using the lastIndexOf() method, we are trying to retrieve the index of the “to” substring.
import java.lang.*; public class Index { public static void main(String[] args) { // instantiating the StringBuffer StringBuffer sb = new StringBuffer("Welcome to TutorialsPoint"); System.out.println("The given string is: " + sb); // initialize the substring and fromIndex value String str = "to"; System.out.println("The substring 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 substring 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 StringBuffer with the value “HelloWorld”. Using the lastIndexOf() method, we are trying to retrieve the index of the null value.
import java.lang.*; public class Index { public static void main(String[] args) { try { //instantiating the StringBuffer StringBuffer sb = new StringBuffer("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:2628) at java.base/java.lang.AbstractStringBuilder.lastIndexOf(AbstractStringBuilder.java:1521) at java.base/java.lang.StringBuffer.lastIndexOf(StringBuffer.java:698) at 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 StringBuffer 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.
import java.lang.*; public class Index { public static void main(String[] args) { //instantiating the StringBuffer StringBuffer sb = new StringBuffer("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 StringBuffer with the value “TutorialsPoint”. Using the lastIndexof() method, we are trying to retrieve the index of the “als” substring from index 10.
import java.lang.*; public class Index { public static void main(String[] args) { //instantiating the StringBuffer StringBuffer sb = new StringBuffer("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