How to clear (empty) a StringBuilder in Java

1 Answer

0 votes
public class MyClass {
    public static void main(String args[]) throws Exception {  
        StringBuilder sb = new StringBuilder("java");
        
        System.out.println(sb);
        System.out.println(sb.length());
        
        sb.setLength(0);  

        System.out.println(sb);
        System.out.println(sb.length()); 
    }
}

 
 
 
 
/*
run:
   
java
4

0
   
*/

 



answered May 13, 2016 by avibootz
edited Nov 5, 2023 by avibootz

Related questions

1 answer 175 views
1 answer 201 views
201 views asked Dec 9, 2020 by avibootz
1 answer 166 views
166 views asked Jul 22, 2020 by avibootz
1 answer 155 views
1 answer 208 views
208 views asked Jan 20, 2022 by avibootz
...