Skip to Content

StringBuffer Reset Java Example

  1. /*
  2.         StringBuffer Reset Java Example
  3.         This example shows how to reset StringBuffer object to null or empty vale using delete
  4.         method.
  5. */
  6.  
  7. public class StrinBufferResetExample {
  8.        
  9.         public static void main(String[] args) {
  10.                
  11.                 //create StringBuffer object
  12.                 StringBuffer sbf = new StringBuffer("Hello World!");
  13.                
  14.                 System.out.println("StringBuffer content: " + sbf) ;
  15.                
  16.                 /*
  17.                  * To reset StringBuffer to empty value, user
  18.                  * StringBuffer delete() method of StringBuffer class.
  19.                  */
  20.                
  21.                 sbf.delete(0, sbf.length());
  22.                
  23.                 System.out.println("StringBuffer content after reset:" + sbf);
  24.         }
  25. }
  26.  
  27. /*
  28. Output of above given Java StringBuffer Reset example would be
  29. StringBuffer content: Hello World!
  30. StringBuffer content after reset:
  31. */