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 256 views
1 answer 296 views
296 views asked Dec 9, 2020 by avibootz
1 answer 258 views
258 views asked Jul 22, 2020 by avibootz
1 answer 246 views
1 answer 303 views
303 views asked Jan 20, 2022 by avibootz
...