How to remove the last two characters from StringBuilder in Java

1 Answer

0 votes
public class MyClass {
    public static void main(String args[]) {
        StringBuilder sb = new StringBuilder("java c c++ c# python");
        
        int len = sb.length();
        
        if (len > 2) {
            sb.setLength(len - 2);
        }
        
        System.out.println(sb);
    }
}
 
  
  
  
/*
run:
       
java c c++ c# pyth
    
*/

 



answered Oct 5, 2023 by avibootz

Related questions

1 answer 103 views
1 answer 231 views
1 answer 128 views
1 answer 139 views
1 answer 173 views
...