
- 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 Useful Resources
- Java.lang - Useful Resources
- Java.lang - Discussion
Java StringBuffer indexOf() Method
The Java StringBuffer indexOf() method is used to retrieve the index of the first occurrence of the specified character in a String or StringBuffer, and the method is case-sensitive, which means that the string "A" and "a" are two different values.
The indexOf() method accepts a parameter as a string that holds the value of the substring. It returns an integer value, if the given value is not present in the current string, it returns -1. The method throws an exception if the given string value is null.
The indexOf() 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 indexOf() method −
public int indexOf(String str) public int indexOf(String str, int fromIndex)
Parameters
- str − This is the substring value.
- fromIndex − This is the index from which to start the search.
Return Value
This method returns the index of the first character of the first such substring if the string argument occurs as a substring within this object. If it does not occur as a substring, -1 is returned.
If you pass value to the fromIndex parameter, this method returns the index of the first occurrence of the specified substring, starting at the specified index.
Example: Getting index of a Substring
If the given string value is not null and exists in the current string, then this method returns the position of it.
In this program, we are creating a StringBuffer object with the value Welcome to TutorialsPoint. Then, using the indexOf() method, we are trying to retrieve the index of the substring to.
package com.tutorialspoint.StringBuffer; 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 String str = "to"; System.out.println("The sustring is: " + str); //using the indexOf() method System.out.println("The position of " + str + " is: " + sb.indexOf(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 position of to is: 8
Example: Facing NullPointerException while Getting index of a null
If the given string value is null the indexOf() method throws a NullPointerException.
In the following example, we are instantiating a StringBuffer with the value null. Using the indexOf() method, we are trying to retrieve the position of the first occurrence of the specified character in the StringBuffer. Since the given string value is null, the method throws an exception.
package com.tutorialspoint.StringBuffer; public class Index { public static void main(String[] args) { try { //instantiating the StringBuffer StringBuffer sb = new StringBuffer(null); System.out.println("The given string is: " + sb); //initialize the substring String str = "hello"; System.out.println("The sustring is: " + str); //using the indexOf() method System.out.println("The position of " + str + " is: " + sb.indexOf(str)); } catch(NullPointerException e) { e.printStackTrace(); System.out.println("Exception: " + e); } } }
Output
Following is the output of the above program −
java.lang.NullPointerException: Cannot invoke "String.length()" because "str" is null at java.base/java.lang.AbstractStringBuilder.(AbstractStringBuilder.java:105) at java.base/java.lang.StringBuffer. (StringBuffer.java:131) at com.tutorialspoint.StringBuffer.Index.main(Index.java:6) Exception: java.lang.NullPointerException: Cannot invoke "String.length()" because "str" is null
Example: Getting index of a Non-Existing Substring
If the given string value does not found in the StringBuffer, the indexOf() method returns -1.
In the following program, we are creating an object of the StringBuffer with the value TutorialsPoint. Then, using the indexOf() method, we try to retrieve the position of the tutorix in the given StringBuffer.
package com.tutorialspoint.StringBuffer; 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 String str = "tutorix"; System.out.println("The sustring is: " + str); //using the indexOf() method System.out.println("The position of " + str + " is: " + sb.indexOf(str)); } }
Output
The above program produces the following output −
The given string is: TutorialsPoint The sustring is: tutorix The position of tutorix is: -1
Example: Getting index of a Substring
If the given string value is not-null and the fromIndex value is positive, the indexOf() method returns the position of the substring.
In this example, we are instantiating the StringBuffer with the value Java Programming Language. Then using the indexOf() method, we are trying to retrieve the language substring position from the index 10.
package com.tutorialspoint.StringBuffer; 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 = "Language"; 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.indexOf(str, fromIndex)); } }
Output
After executing the above program, the program will produce the following output −
The given string is: Java Programming Language The sustring is: Language The fromIndex value is: 10 The position of Language is: 17