java.lang.StringBuffer indexOf(String str,int fromIndex)
Syntax:
public int indexOf(String str,int fromIndex)
Java Code Example :
This java example source code demonstrates the use of indexOf(String str,int fromIndex) method of StringBuffer class. Initially the code assigns a string “javatutorialhq.com” as initial contents of the string buffer. Then we use the indexof method to get the starting index of string “a”. This method the same as that of indexOf(String str) of the same class, its just that this method has a specified index to start looking for the specified string.
This is very useful to work with substring method of StringBuffer class. On this example we will also learn how to use the indexof method to work with substring.
package com.javatutorialhq.java.examples; /* * This example source code demonstrates the use of * indexOf(String str,int fromIndex) method of StringBuffer class */ public class StringBufferIndexOfStringFromIndex { public static void main(String[] args) { // initialize the StringBuffer object StringBuffer sb = new StringBuffer("javatutorialhq.com"); System.out.println("Contents of buffer:" + sb); // get the index of "a" string starting from index 10 from the stringbuffer object String str = "a"; int fromIndex = 10; int index = sb.indexOf(str, fromIndex); System.out.println("index:" + index); // incorporate substring method. Get a part of stringbuffer // from the result of indexOf lookup until the end of this stringbuffer String result = sb.substring(index,sb.length()); System.out.println("Result:" + result); } }
Sample Output :
Running the above example source code will give the following output