indexOf(): This method is available in java.lang.StringBuffer class of Java.
Syntax:
int java.lang.StringBuffer.indexOf(String str)
This method takes one argument of type String as its parameter. This method returns the index within this string of the first occurrence of the specified substring.
Parameters: One parameter is required for this method.
str: the substring to search for.
Returns: the index of the first occurrence of the specified substring, or -1 if there is no such occurrence.
For Example:
StringBuffer str = new StringBuffer("Hello World")
String str1 = "o"
str.indexOf(str1) = > It returns 4.
Approach
Java
public class IndexOf {public static void main(String[] args) {StringBuffer str = new StringBuffer("Hello World");String str1 = "o";System.out.println(str.indexOf(str1));}}
Output:
4
No comments:
Post a Comment