StringBuilder length() in Java with Examples (original) (raw)

Last Updated : 15 Oct, 2018

The length() method of StringBuilder class returns the number of character the StringBuilder object contains. The length of the sequence of characters currently represented by this StringBuilder object is returned by this method.

Syntax:

public int length()

Return Value: This method returns length of sequence of characters contained by StringBuilder object.

Below programs demonstrate the length() method of StringBuilder Class:

Example 1:

class GFG {

`` public static void main(String[] args)

`` {

`` StringBuilder

`` str

`` = new StringBuilder( "WelcomeGeeks" );

`` System.out.println( "String = "

`` + str.toString());

`` int length = str.length();

`` System.out.println( "length of String = "

`` + length);

`` }

}

Output:

String = WelcomeGeeks length of String = 12

Example 2:

class GFG {

`` public static void main(String[] args)

`` {

`` StringBuilder

`` str

`` = new StringBuilder( "India is Great" );

`` System.out.println( "String = "

`` + str.toString());

`` int length = str.length();

`` System.out.println( "length of String = "

`` + length);

`` }

}

Output:

String = India is Great length of String = 14

Reference:
https://docs.oracle.com/javase/10/docs/api/java/lang/StringBuilder.html#length()

Similar Reads