HOWTO · Java

How to Clear a StringBuilder in Java

Learn how to clear or change a Java StringBuilder with setLength(0), reassignment, and delete().

On this page

To clear a StringBuilder while keeping the same object, call setLength(0). To make a variable refer to a fresh builder, assign it a new StringBuilder; this does not change the old object for any other references. Use delete(start, end) when you want to remove a particular range, or the full range to clear all content.

Clear a StringBuilder with setLength(0)

setLength(0) sets the builder’s logical length to 0. The variable still refers to the same StringBuilder, so other references to that object also see the reset. This is useful when the builder should be reused for another value.

Run this example:

public class Main {
  public static void main(String[] args) {
    StringBuilder builder = new StringBuilder("Hello");
    builder.setLength(0);
    builder.append("Java");
    System.out.println(builder);
  }
}
Java

After the reset, the appended text is the builder’s content. The capacity is a separate property from length; this operation resets the logical content and does not promise a particular memory or performance benefit.

Clear a StringBuilder by assigning a new instance

Assigning new StringBuilder() changes which object the variable refers to. It does not clear or modify the previous object, so aliases continue to refer to that object’s existing contents. Choose this when the variable should start with a separate builder and other references should retain the old value.

public class Main {
  public static void main(String[] args) {
    StringBuilder current = new StringBuilder("draft");
    StringBuilder alias = current;
    current = new StringBuilder();
    current.append("final");
    System.out.println(current);
    System.out.println(alias);
  }
}
final
draft

Here, current refers to the new builder, while alias still refers to the builder containing draft. If the goal is for every reference to observe the same reset, mutate the original object with setLength(0) instead.

Remove all or part of the content with delete()

delete(start, end) removes characters from the inclusive start index up to, but not including, end. For example, deleting indexes 1 through 3 from "abcdef" removes "bcd" and leaves "aef". This makes delete() useful when only a section should be removed.

To clear the entire current content with delete(), use the range from zero to the builder’s current length:

public class Main {
  public static void main(String[] args) {
    StringBuilder builder = new StringBuilder("abcdef");
    builder.delete(1, 4);
    System.out.println(builder);
    builder.delete(0, builder.length());
    System.out.println(builder.length());
  }
}
aef
0

The first call removes only the selected range. The second call computes the end index before deletion and removes everything then present. Using the current length avoids hard-coding an end position when the content can vary.

Choose between clearing and replacing a StringBuilder

Use setLength(0) when the existing object should be reset, especially if other code holds a reference to it. Use a new StringBuilder when only the current variable should point to a fresh object and aliases should keep the old content. Use delete(start, end) for a selected portion; delete(0, builder.length()) clears all content through range deletion.

These operations have different effects on object identity. Resetting or deleting changes the contents seen through every reference to that object. Reassignment changes only the variable being assigned. Choose based on which references should see the change, and then append or build the next value as needed.