Way2Java

indexOf() StringBuffer Example

public class Demo
{
  public static void main(String args[])
  {				            // hello comes in 3 places	
   StringBuffer sb1 = new StringBuffer("123hello456hello789hello");    
                                            // to find 1st occurrence of hello
   int x = sb1.indexOf("hello");   
   System.out.println("First Occurrence of hello: " + x);

                                            // to find first occurrence of hello starting from 9th index number
   x = sb1.indexOf("hello", 9);   
   System.out.println("Occurrence of hello from index 9: " + x);

   x = sb1.indexOf("world");   	            // world does not exist
   System.out.println("Occurrence of world which does not exist: " + x);
 }
}



Output screenshot on indexOf() StringBuffer example

Pass your comments and suggestions on this tutorial "indexOf() StringBuffer Example".