GeezerVisonThe World Wide Web, restored to 800 pixels of glory. |
| Home|About|Gaming cabinet|Archives|How it works|Starter sites |
GeezerVison is showing www.tutorialspoint.com — Java StringBuffer length() Method
Selected Reading
Java StringBuffer length() MethodThe Java StringBuffer length() method is used to get the length of the StringBuffer object. A length is simply the number of characters in the String/StringBuffer. If the current StringBuffer 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. SyntaxFollowing is the syntax of the Java StringBuffer length() method − public int length() Parameters
Return ValueThis method returns the length of the sequence of characters currently represented by this object. Example: Getting Length of the String of StringBufferIf 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 StringBuffer with the value TutorialsPoint. Using the length() method, we are retrieving the length of the current StringBuffer object. public class Demo {
public static void main(String[] args) {
//creating an object of the StringBuffer
StringBuffer sb = new StringBuffer("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);
}
}
OutputOn executing the above program, it will produce the following result − The given string is: TutorialsPoint The length of the 'TutorialsPoint' is: 14 Example: Getting Length of Empty String of StringBufferIf the current StringBuffer is an empty string, the length() method returns 0. In the following program, we are instantiating the StringBuffer with an empty value. Using the length() method, we are trying to retrieve the length of this StringBuffer object. import java.util.stream.IntStream;
public class Demo {
public static void main(String[] args) {
//creating an object of the StringBuffer
StringBuffer sb = new StringBuffer("");
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);
}
}
OutputFollowing is the output of the above program − The given string is: The length of the '' is: 0 Example: Getting Length of the String of StringBufferIn the following example, we are creating an object of the StringBuffer 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. public class Demo {
public static void main(String[] args) {
//creating an object of the StringBuffer
StringBuffer sb = new StringBuffer("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) + " ");
}
}
}
OutputThe 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: Getting Length of the String with Whitespaces of StringBufferIf 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 StringBuffer with an empty value, but it contains 4 white-spaces. Using the length() method, we are trying to retrieve its length. public class Demo {
public static void main(String[] args) {
//instantiating StringBuffer
StringBuffer sb = new StringBuffer(" ");
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);
}
}
OutputAfter executing the above program, it will produce the following output − The given string is: The length of the ' ' is: 3 java_lang_stringbuffer.htm Advertisements
|
| You are visitor 01402503 — GeezerVison does not store the pages you look up. Best viewed with Netscape Navigator 4.7 or Internet Explorer 6 at 800×600. No cookies required. |