Java StringBuilder setLength() Method



The Java StringBuilder setLength() method is used to set/change the length of a StringBuilder object.

On invoking this method by passing an integer value representing the new length, the current object is changed to a new object (StringBuilder) whose length is same as the given argument.

A length is nothing but the count of the characters in the given sequence. We have length() method in java, to find the length of the given sequence.

If the newLength argument is greater than or equal to the current length, sufficient null characters ('\u0000') are appended so that length becomes the newLength argument.

The setLength() method accepts a parameter as an integer that holds the value of the newLength. It throws an exception if the given newLength value is negative.

Syntax

Following is the syntax of the Java StringBuilder setLength() method −

public void setLength(int newLength)

Parameters

  • newLength − This is the new length.

Return Value

This method does not return any value.

Example

If the given newLength argument value is positive, the setLength() method sets the new length of the given sequence.

In the following program, we are instantiating the StringBuilder class with the value of “TutorialsPoint”. Using the setLength() method, we are trying to set the new length of the given sequence as 20.

package com.tutorialspoint.StringBuilder;
public class SetLength {
   public static void main(String[] args) {
      
      //instantiating the StringBuilder class
      StringBuilder sb = new StringBuilder("TutorialsPoint");
      System.out.println("StringBuilder: " + sb);
      System.out.println("Before setting the new length the original length is: " + sb.length());
      
      //initialize the newLength argument value
      int newLength = 20;
      System.out.println("The given newLength argument value is: " + newLength);
      
      //using the setLength() method
      sb.setLength(newLength);
      System.out.println("After setting the new length the string length is: " + sb.length());
   }
}

Output

On executing the above program, it will produce the following result −

StringBuilder: TutorialsPoint
Before setting the new length the original length is: 14
The given newLength argument value is: 20
After setting the new length the string length is: 20

Example

If the given newLength argument value is negative, the setLength() method throws an IndexOutOfBoundsException.

In the following example, we are creating an object of the StringBuilder class with the value “HelloWorld”. Using the setLength() method, we are trying to set the sequence length as -5.

package com.tutorialspoint.StringBuilder;
public class SetLength {
   public static void main(String[] args) {
      try {
         
         //create an object of the StringBuilder class
         StringBuilder sb = new StringBuilder("HelloWorld");
         System.out.println("StringBuilder: " + sb);
         System.out.println("Before setting the original length is: " + sb.length());
         
         //initialize the newLength argument value
         int newLength = -5;
         System.out.println("The given newLength argument value is: " + newLength);
         
         //using the setLength() method
         sb.setLength(newLength);
         System.out.println("After setting is the new length of string is: " + sb.length());
      } catch(IndexOutOfBoundsException e) {
         e.printStackTrace();
         System.out.println("Exception: " + e);
      }
   }
}

Output

Following is the output of the above program −

StringBuilder: HelloWorld
Before setting the original length is: 10
The given newLength argument value is: -5
java.lang.StringIndexOutOfBoundsException: String index out of range: -5
	at java.base/java.lang.AbstractStringBuilder.setLength(AbstractStringBuilder.java:319)
	at java.base/java.lang.StringBuilder.setLength(StringBuilder.java:91)
	at com.tutorialspoint.StringBuilder.SetLength.main(SetLength.java:13)
Exception: java.lang.StringIndexOutOfBoundsException: String index out of range: -5

Example

If the given string length is zero, this method sets the new length of the given sequence.

In the following program, we are instantiating the StringBuilder class with an empty value. Then, using the setLength() method, we are trying to set the new length of the given empty sequence as 5.

package com.tutorialspoint.StringBuilder;
public class SetLength {
   public static void main(String[] args) {
      
      //instantiating the StringBuilder class
      StringBuilder sb = new StringBuilder();
      System.out.println("StringBuilder: " + sb);
      System.out.println("Before setting the new length the original length is: " + sb.length());
      
      //initialize the newLength argument value
      int newLength = 5;
      System.out.println("The given newLength argument value is: " + newLength);
      
      //using the setLength() method
      sb.setLength(newLength);
      System.out.println("After setting the new length the string length is: " + sb.length());
   }
}

Output

The above program, produces the following results −

StringBuilder: 
Before setting the new length the original length is: 0
The given newLength argument value is: 5
After setting the new length the string length is: 5
java_lang_stringbuilder.htm
Advertisements