- Trending Categories
Data Structure
Networking
RDBMS
Operating System
Java
MS Excel
iOS
HTML
CSS
Android
Python
C Programming
C++
C#
MongoDB
MySQL
Javascript
PHP
Physics
Chemistry
Biology
Mathematics
English
Economics
Psychology
Social Studies
Fashion Studies
Legal Studies
- Selected Reading
- UPSC IAS Exams Notes
- Developer's Best Practices
- Questions and Answers
- Effective Resume Writing
- HR Interview Questions
- Computer Glossary
- Who is Who
How can I clear or empty a StringBuilder in Java.
You can either setLength to be 0 or create a new StringBuilder() instance. See the example below −
Example
public class Tester { public static void main(String[] args) { StringBuilder builder = new StringBuilder(); builder.append("sample"); System.out.println(builder.toString()); builder.setLength(0); System.out.println(builder.toString()); builder.append("sample"); System.out.println(builder.toString()); } }
Output
sample sample
- Related Articles
- Clear a StringBuilder in C#
- How can we compare a StringBuilder with StringBuffer in Java?
- How do I empty a list in Java?
- How can I clear console using C++?
- How can I search a character or substring in java?
- How can we check if a JSON object is empty or not in Java?\n
- How can I clear text of a textbox using Python Selenium WebDriver?
- How can we clear all selections in Java Swing JList?
- How do I check if a column is empty or null in MySQL?
- How can I remove all empty values when I explode a string using PHP?
- How can I check JavaScript arrays for empty strings?
- Check whether a NavigableMap empty or not in Java
- Checking for Null or Empty in Java.
- Java StringBuilder class.
- Converting a StringBuilder to String in Java

Advertisements