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
- 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 3 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 indexOf() method in Java with Examples In StringBuilder class, there are two types of indexOf() method depending upon the parameters passed to it. indexOf(String str) The indexOf(String str) method of StringBuilder class is the inbuilt method used to return the index within the String for first occurrence of passed substring as parameter 4 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
- StringBuilder trimToSize() method in Java with Examples The trimToSize() method of StringBuilder class is the inbuilt method used to trims the capacity used for the character sequence of StringBuilder object. If the buffer used by StringBuilder object is larger than necessary to hold its current sequence of characters, then this method is called to resiz 2 min read
- StringBuilder getChars() in Java with Examples The getChars(int srcBegin, int srcEnd, char[] dst, int dstBegin) method of StringBuilder class copies the characters starting at the given index:srcBegin to index:srcEnd-1 from String contained by StringBuilder into an array of char passed as parameter to function. The characters are copied from Str 3 min read
- StringReader skip(long) method in Java with Examples The skip(long) method of StringReader Class in Java is used to skip the specified number of characters on the stream. This number of characters is specified as the parameter. If it reaches the end of the stream by skipping, it will block the stream till it gets some characters, or throw IOException. 3 min read