stringbuffer java program with examples


StringBuffer Java Program


public class StringBufferExmp{
     public static void main(String []args){

StringBuffer sb1=new StringBuffer("Tech "); 

sb1.append("World");                  //concatenating string
System.out.println(sb1);             //printing string  Tech World

sb1.insert(1,"Java");              //string is changing now
System.out.println(sb1); //printing string TJavaech World
                                                               
sb1.replace(1,3,"Java"); 
System.out.println(sb1); //printing TJavavaech World                           

sb1.delete(1,3); 
System.out.println(sb1); // printing Tvavaech World 

sb1.reverse(); 
System.out.println(sb1); // printing dlroW hceavavT                                                                               

 System.out.println(sb1.capacity()); // printing  21

     }
}

Output:

Tech World
TJavaech World
TJavavaech World 
Tvavaech World
dlroW hceavavT   
21


EmoticonEmoticon