String Buffer Program in Java

  1. import java.io.*;  
  2.   
  3. public class stringBuffer{  
  4.     public static void main(String[] args) throws Exception{  
  5.         BufferedReader in = new BufferedReader(new InputStreamReader(System.in));  
  6.         String str;  
  7.         try{  
  8.             System.out.print("Enter your name: ");  
  9.             str = in.readLine();  
  10.             str += ", This is the example of SringBuffer class and it's functions.";  
  11.   
  12.             //Create a object of StringBuffer class  
  13.             StringBuffer strbuf = new StringBuffer();  
  14.             System.out.print(strbuf.length());  
  15.             strbuf.append(str);  
  16.             System.out.println(strbuf);  
  17.             strbuf.delete(0,str.length());  
  18.               
  19.             //append()  
  20.             strbuf.append("Hello");  
  21.             strbuf.append("World");                         //print HelloWorld  
  22.             System.out.println(strbuf);  
  23.               
  24.             //insert()  
  25.             strbuf.insert(5,"_Java ");                      //print Hello_Java World  
  26.             System.out.println(strbuf);  
  27.               
  28.             //reverse()  
  29.             strbuf.reverse();  
  30.             System.out.print("Reversed string : ");  
  31.             System.out.println(strbuf);                     //print dlroW avaJ_olleH  
  32.             strbuf.reverse();  
  33.             System.out.println(strbuf);                     //print Hello_Java World  
  34.               
  35.             //setCharAt()  
  36.             strbuf.setCharAt(5,' ');  
  37.             System.out.println(strbuf);                     //prit Hello Java World  
  38.               
  39.             //charAt()  
  40.             System.out.print("Character at 6th position : ");  
  41.             System.out.println(strbuf.charAt(6));           //print J  
  42.       
  43.             //substring()  
  44.             System.out.print("Substring from position 3 to 6 : ");  
  45.             System.out.println(strbuf.substring(3,7));      //print lo J  
  46.                           
  47.             //deleteCharAt()  
  48.             strbuf.deleteCharAt(3);  
  49.             System.out.println(strbuf);                     //print Helo java World  
  50.               
  51.             //capacity()  
  52.             System.out.print("Capacity of StringBuffer object : ");  
  53.             System.out.println(strbuf.capacity());          //print 21  
  54.               
  55.             //delete() and length()  
  56.             strbuf.delete(6,strbuf.length());                 
  57.             System.out.println(strbuf);                     //no anything  
  58.         }  
  59.         catch(StringIndexOutOfBoundsException e){  
  60.             System.out.println(e.getMessage());  
  61.         }  
  62.     }