Java StringBuilder trimToSize() Method
Example
Reduce the capacity of a StringBuilder to fit its text:
StringBuilder sb = new StringBuilder("Hello");
System.out.println(sb.capacity());
sb.trimToSize();
System.out.println(sb.capacity());
Definition and Usage
The trimToSize() method reduces the capacity of a StringBuilder, so it is not larger than needed for the current text. This can save memory.
Syntax
public void trimToSize()
Parameter Values
None.
Related Pages
❮ StringBuilder Methods