Java StringBuildersetLength Examples

In this tutorial, we will learn about the Java StringBuilder.setLength() function, and learn how to use this function to set a specific length for this StringBuilder sequence, with the help of examples.

setLengthint newLength

StringBuilder.setLength() sets the length of this StringBuilder sequence.

Syntax

The syntax of setLength() function is

setLength(int newLength)

where

Parameter Description
newLength New length for this StringBuilder sequence.

Returns

The function returns void. Means, the function returns nothing.

Example 1 setLengthnewLength newLength length

In this example, we will take a StringBuilder of length 6, and set its length to a new value of 10. Since, length of StringBuilder sequence is less than the new length, setLength() appends null characters to make it to the new length.

Java Program

public class Example { 
    public static void main(String[] args) { 
        StringBuilder stringBuilder = new StringBuilder("abcdef");
        System.out.println("Value  before setting : \"" + stringBuilder.toString()+"\"");
        System.out.println("Length before setting : " + stringBuilder.length());
        
        int newLength = 10;
        stringBuilder.setLength(newLength);
        System.out.println("Value  after setting : \"" + stringBuilder.toString()+"\"");
        System.out.println("Length after setting : " + stringBuilder.length()); 
    }
}

Output

Value  before setting : "abcdef"
Length before setting : 6
Value  after setting : "abcdef    "
Length after setting : 10

Example 2 setLengthnewLength newLength length

In this example, we will set a new length for StringBuilder such that the new length is less than the actual length of StringBuilder. In this case, setLength() trims out the excess characters and sets the length of StringBuilder to specified length.

Java Program

public class Example { 
    public static void main(String[] args) { 
        StringBuilder stringBuilder = new StringBuilder("abcdef");
        System.out.println("Value  before setting : \"" + stringBuilder.toString()+"\"");
        System.out.println("Length before setting : " + stringBuilder.length());
        
        int newLength = 4;
        stringBuilder.setLength(newLength);
        System.out.println("Value  after setting : \"" + stringBuilder.toString()+"\"");
        System.out.println("Length after setting : " + stringBuilder.length());
    }
}

Output

Value  before setting : "abcdef"
Length before setting : 6
Value  after setting : "abcd"
Length after setting : 4

Example 3 setLengthnewLength newLength 0

A negative value for newLength makes setLength() throw ava.lang.StringIndexOutOfBoundsException.

Java Program

public class Example { 
    public static void main(String[] args) { 
        StringBuilder stringBuilder = new StringBuilder("abcdef");
        System.out.println("Value  before setting : \"" + stringBuilder.toString()+"\"");
        System.out.println("Length before setting : " + stringBuilder.length());
        
        int newLength = -5;
        stringBuilder.setLength(newLength);
        System.out.println("Value  after setting : \"" + stringBuilder.toString()+"\"");
        System.out.println("Length after setting : " + stringBuilder.length());
    }
}

Output

Value  before setting : "abcdef"
Length before setting : 6
Exception in thread "main" java.lang.StringIndexOutOfBoundsException: String index out of range: -5
	at java.base/java.lang.AbstractStringBuilder.setLength(Unknown Source)
	at java.base/java.lang.StringBuilder.setLength(Unknown Source)
	at Example.main(Example.java:8)

Conclusion

In this Java Tutorial, we have learnt the syntax of Java StringBuilder.setLength() function, and also learnt how to use this function with the help of examples.