StringBuilder length() in Java with Examples Last Updated : 15 Oct, 2018 Summarize Comments Improve Suggest changes Share Like Article Like Report 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: Java // Java program to demonstrate // the length() Method. class GFG { public static void main(String[] args) { // create a StringBuilder object // with a String pass as parameter StringBuilder str = new StringBuilder("WelcomeGeeks"); // print string System.out.println("String = " + str.toString()); // get length of StringBuilder object int length = str.length(); // print length System.out.println("length of String = " + length); } } Output: String = WelcomeGeeks length of String = 12 Example 2: Java // Java program to demonstrate // the length() Method. class GFG { public static void main(String[] args) { // create a StringBuilder object // with a String pass as parameter StringBuilder str = new StringBuilder("India is Great"); // print string System.out.println("String = " + str.toString()); // get length of StringBuilder object int length = str.length(); // print 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() Comment More infoAdvertise with us Next Article StringBuilder lastIndexOf() method in Java with Examples A AmanSingh2210 Follow Improve Article Tags : Java java-basics Java-Strings Java-Functions Java-StringBuilder +1 More Practice Tags : JavaJava-Strings Similar Reads StringBuilder setLength() in Java with Examples The setLength(int newLength) method of StringBuilder is used to set the length of the character sequence equal to newLength.For every index k greater than 0 and less than newLength. If the newLength passed as argument is less than the old length, the old length is changed to the newLength.If the new 2 min read StringBuilder replace() in Java with Examples The replace(int start, int end, String str) method of StringBuilder class is used to replace the characters in a substring of this sequence with characters in the specified String. The substring begins at the specified index start and extends to the character at index end - 1 or to the end of the se 3 min read StringBuilder toString() method in Java with Examples The toString() method of the StringBuilder class is the inbuilt method used to return a string representing the data contained by StringBuilder Object. A new String object is created and initialized to get the character sequence from this StringBuilder object and then String is returned by toString( 3 min read StringBuilder charAt() in Java with Examples The charAt(int index) method of StringBuilder Class is used to return the character at the specified index of String contained by StringBuilder Object. The index value should lie between 0 and length()-1. Syntax: public char charAt(int index) Parameters: This method accepts one int type parameter in 3 min read StringBuilder lastIndexOf() method in Java with Examples In StringBuilder class, there are two types of lastIndexOf() method depending upon the parameters passed to it. lastIndexOf(String str) The lastIndexOf(String str) method of StringBuilder class is the inbuilt method used to return the index within the String for last occurrence of passed substring a 4 min read StringBuilder delete() in Java with Examples The delete(int start, int end) method of StringBuilder class removes the characters starting from index start to index end-1 from String contained by StringBuilder. This method takes two indexes as a parameter first start represents index of the first character and endIndex represents index after th 3 min read Like