Java StringBuilder setLength() Method Example

Java StringBuilder setLength(int newLength) method sets the length of the character sequence.

Java String valueOf() Method Example

Example to reset the length of the String "javaguides" to 0 using setLength() method.
public class SetLengthExample {
     public static void main(String[] args) {
         StringBuilder builder = new StringBuilder("javaguides");
         System.out.println("Before set length to 0 : " + builder.length());
         builder.setLength(0);
         System.out.println("After set length to 0 : " + builder.length());
    }
}
Output:
Before set length to 0 : 10
After set length to 0 : 0

Reference



Comments