Java - StringBuilder length() Method



The Java StringBuilder length() method is used to get the length of the StringBuilder object. A length is simply the number of characters in the String/StringBuilder. If the current StringBuilder object is empty, this method returns zero.

The length() method does not accept any parameter. It does not throw any exception while retrieving the length of the string.

Note − If the string is an empty string but contains some white-spaces, then the length() method counts the white-spaces and returns it as the length of the string.

Syntax

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

public int length()

Parameters

  • It does not accept any parameter.

Return Value

This method returns the length of the sequence of characters currently represented by this object.

Example

If the value of the StingBuilder object is not null, this method returns the length of the string.

In the following example, we are creating an object of the StringBuilder with the value “TutorialsPoint”. Using the length() method, we are retrieving the length of the current StringBuilder object.

package com.tutorialspoint.String;
import java.lang.*;
public class Demo {
   public static void main(String[] args) {
      
      //creating an object of the StringBuilder
      StringBuilder sb = new StringBuilder("TutorialsPoint");
      System.out.println("The given string is: " + sb);
      
      //using the length() method
      int length = sb.length();
      System.out.println("The length of the '" + sb + "' is: " + length);
   }
}

Output

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

The given string is: TutorialsPoint
The length of the 'TutorialsPoint' is: 14

Example

If the current StringBuilder is an empty string, the length() method returns 0.

In the following program, we are instantiating the StringBuilder with an empty value. Using the length() method, we are trying to retrieve the length of this StringBuilder object.

package com.tutorialspoint.String;
import java.lang.*;
import java.util.stream.IntStream;
public class Demo {
   public static void main(String[] args) {
      
      //creating an object of the StringBuilder
      StringBuilder sb = new StringBuilder("");
      System.out.println("The given string is: " + sb);
      
      //using the length() method
      int length = sb.length();
      System.out.println("The length of the '" + sb + "' is: " + length);
   }
}

Output

Following is the output of the above program −

The given string is: 
The length of the '' is: 0

Example

In the following example, we are creating an object of the StringBuilder with the value “HelloWorld”. Then, we are using the for loop to iterate through the length() of the string. Using the charAt() method, we are trying to print each character of the current object.

package com.tutorialspoint.String;
import java.lang.*;
public class Demo {
   public static void main(String[] args) {
      
      //creating an object of the StringBuilder
      StringBuilder sb = new StringBuilder("HelloWorld");
      System.out.println("The given string is: " + sb);
      
      //using the length() method
      int length = sb.length();
      System.out.println("The length of the '" + sb + "' is: " + length);
      
      //using for loop
      System.out.print("The string characters are: ");
      for(int i = 0; i<length; i++) {
         
         //using the charAt() method
         System.out.print(sb.charAt(i) + " ");
      }
   }
}

Output

The above program, produces the following results −

The given string is: HelloWorld
The length of the 'HelloWorld' is: 10
The string characters are: H e l l o W o r l d

Example

If the current object just contains some white-spaces, the length() method counts the white space and returns it as string length.

In the following program, we are instantiating the StringBuilder with an empty value, but it contains 4 white-spaces. Using the length() method, we are trying to retrieve its length.

package com.tutorialspoint.String;
import java.lang.*;
public class Demo {
   public static void main(String[] args) {
      
      //instantiating  StringBuilder
      StringBuilder sb = new StringBuilder("   ");
      System.out.println("The given string is: " + sb);
      
      //using the length() method
      int length = sb.length();
      System.out.println("The length of the '" + sb + "' is: " + length);
   }
}

Output

After executing the above program, it will produce the following output −

The given string is:    
The length of the '   ' is: 4
java_lang_stringbuilder.htm
Advertisements