Clear the String Buffer in Java

Exercise:

Write a Java Program to clear the string buffer.

1. Clear using delete()

Click Here to View the Solution!
public class ClearStringBuffer {
    public static void main(String[] args) {

        // create a string buffer
        StringBuffer str = new StringBuffer();
        str.append("Coding");
        str.append(" is");
        str.append(" easy.");
        System.out.println("StringBuffer: " + str);

        // clear the string
        str.delete(0, str.length());
        System.out.println("Updated StringBuffer: " + str);
    }
}
Click Here to View the Output!
StringBuffer: Coding is easy.                                                                 
Updated StringBuffer:
Click Here to View the Explanation!
  • In this program, an object str of the String Buffer class has been created.
  • Three strings are appended to the str object and then the String Buffer is displayed on the screen.
  • Then the delete() method is used to clear all the characters of the String Buffer from the beginning of the buffer till its length.
  • The updated String Buffer is displayed on the screen, which is now empty.

2.Clear using setLegnth()

Click Here to View the Solution!
public class ClearStringBuffer {
    public static void main(String[] args) {
        StringBuffer str = new StringBuffer();
        str.append("Coding");
        str.append(" is");
        str.append(" easy.");
        System.out.println("StringBuffer: " + str);

        // clear the string by setting length=0
        str.setLength(0);
        System.out.println("Updated StringBuffer: " + str);
    }
}
Click Here to View the Output!
StringBuffer: Coding is easy.                                                                 
Updated StringBuffer:
Click Here to View the Explanation!
  • In this program, an object str of the String Buffer class has been created.
  • Three strings are appended to the str object and then the String Buffer is displayed on the screen.
  • Then setLength(0) is used to modify the pattern of characters currently present in the StringBuffer to another character pattern while setting the length of the character pattern to 0. The previous pattern is disposed.
  • The setLength() method shifts the character pattern completely whereas the delete() method removes the characters present in the buffer. Thus, setLength() works much quicker than delete() method.

3.Clear by creating a new object

Click Here to View the Solution!
public class ClearStringBuffer {
    public static void main(String[] args) {
        StringBuffer str = new StringBuffer();
        str.append("Coding");
        str.append(" is");
        str.append(" Easy.");
        System.out.println("StringBuffer: " + str);

        // clear the string using new object
        str = new StringBuffer();
        System.out.println("Updated StringBuffer: " + str);
    }
}
Click Here to View the Output!
StringBuffer: Coding is easy.                                                                 
Updated StringBuffer:
Click Here to View the Explanation!
  • In this program, an object str of the String Buffer class has been created.
  • Three strings are appended to the str object and then the String Buffer is displayed on the screen.
  • new StringBuffer() assigns a new buffer object to the same object name as has been declared earlier.
  • Thus, on accessing this object, it will return an empty buffer since the previous version of the object containing strings cannot be accessed and has been collected by garbage.
  • Because of new object creation without the proper deletion of previous one, this process is not as efficient as the other techniques.