Java: StringBuffer length() method

In this article, we will discuss how to get length of StringBuffer object using StringBuffer’s length() method

 

length() method:

  • This StringBuffer method is used to get length of the StringBuffer or character count
  • That’s sequence of characters or number of characters appended/inserted into StringBuffer

 

Method Signature:

public int length();

 

Returns:

  • Return the length i.e.; character count

 

Examples on length() method:

Generally, length() method of StringBuffer class is used for following purpose,

  1. To get length or character count
  2. for-loop: used to keep boundary condition, while iterating
  3. while-loop: used to keep boundary condition, while iterating
  4. do-while-loop: used to keep boundary condition, while iterating
  5. To create equivalent char[] array

 

Let’s see examples for each listed above cases:

1. To get length or character count of StringBuffer

StringBufferLengthMethod.java

package in.bench.resources.stringbuffer.methods;

public class StringBufferLengthMethod {

	public static void main(String[] args) {

		// StringBuffer object
		StringBuffer sbUrl = new StringBuffer("BenchResources.Net");

		// to check length of the StringBuffer
		int length = sbUrl.length();

		// print to console
		System.out.println("The length of StringBuffer '" + sbUrl + "' is " + length);
	}
}

Output:

The length of StringBuffer 'BenchResources.Net' is 18

 

2. Boundary condition for for-loop while iterating

StringBufferLengthForLoop.java

package in.bench.resources.stringbuffer.methods;

public class StringBufferLengthForLoop {

	public static void main(String[] args) {

		// StringBuffer object
		StringBuffer sbUrl = new StringBuffer("BenchResources.Net");

		// iterating using for-loop
		for(int index = 0; index < sbUrl.length(); index++) {

			System.out.println("StringBuffer character at " + index + "-position is : "
					+ sbUrl.charAt(index));
		}
	}
}

Output:

StringBuffer character at 0-position is : B
StringBuffer character at 1-position is : e
StringBuffer character at 2-position is : n
StringBuffer character at 3-position is : c
StringBuffer character at 4-position is : h
StringBuffer character at 5-position is : R
StringBuffer character at 6-position is : e
StringBuffer character at 7-position is : s
StringBuffer character at 8-position is : o
StringBuffer character at 9-position is : u
StringBuffer character at 10-position is : r
StringBuffer character at 11-position is : c
StringBuffer character at 12-position is : e
StringBuffer character at 13-position is : s
StringBuffer character at 14-position is : .
StringBuffer character at 15-position is : N
StringBuffer character at 16-position is : e
StringBuffer character at 17-position is : t

 

3. Boundary condition for while-loop while iterating

StringBufferLengthWhileLoop.java

package in.bench.resources.stringbuffer.methods;

public class StringBufferLengthWhileLoop {

	public static void main(String[] args) {

		// StringBuffer object
		StringBuffer sbUrl = new StringBuffer("BenchResources.Net");

		// initialize index
		int index = 0;

		// get length
		int length = sbUrl.length();

		// iterating using while-loop
		while(index < length){

			// print to console
			System.out.println("StringBuffer character at " + index + "-position is : "
					+ sbUrl.charAt(index));

			// increment index-value by 1
			index++;
		}
	}
}

Output:

StringBuffer character at 0-position is : B
StringBuffer character at 1-position is : e
StringBuffer character at 2-position is : n
StringBuffer character at 3-position is : c
StringBuffer character at 4-position is : h
StringBuffer character at 5-position is : R
StringBuffer character at 6-position is : e
StringBuffer character at 7-position is : s
StringBuffer character at 8-position is : o
StringBuffer character at 9-position is : u
StringBuffer character at 10-position is : r
StringBuffer character at 11-position is : c
StringBuffer character at 12-position is : e
StringBuffer character at 13-position is : s
StringBuffer character at 14-position is : .
StringBuffer character at 15-position is : N
StringBuffer character at 16-position is : e
StringBuffer character at 17-position is : t

 

4. Boundary condition for do-while-loop while iterating

StringBufferLengthDoWhileLoop.java

package in.bench.resources.stringbuffer.methods;

public class StringBufferLengthDoWhileLoop {

	public static void main(String[] args) {

		// StringBuffer object
		StringBuffer sbUrl = new StringBuffer("BenchResources.Net");

		// initialize index
		int index = 0;

		// get length
		int length = sbUrl.length();

		// iterating using do-while-loop
		do {

			// print to console
			System.out.println("StringBuffer character at " + index + "-position is : "
					+ sbUrl.charAt(index));

			// increment index-value by 1
			index++;
		} while(index < length);
	}
}

Output:

StringBuffer character at 0-position is : B
StringBuffer character at 1-position is : e
StringBuffer character at 2-position is : n
StringBuffer character at 3-position is : c
StringBuffer character at 4-position is : h
StringBuffer character at 5-position is : R
StringBuffer character at 6-position is : e
StringBuffer character at 7-position is : s
StringBuffer character at 8-position is : o
StringBuffer character at 9-position is : u
StringBuffer character at 10-position is : r
StringBuffer character at 11-position is : c
StringBuffer character at 12-position is : e
StringBuffer character at 13-position is : s
StringBuffer character at 14-position is : .
StringBuffer character at 15-position is : N
StringBuffer character at 16-position is : e
StringBuffer character at 17-position is : t

 

Difference between while-loop and do-while-loop:

  • do-while-loop checks boundary condition, after 1st loop iteration (at least 1 iteration is possible)
  • while-loop checks boundary condition even before 1st iteration (no iteration is possible without satisfying loop-entry condition)

 

5. To create equivalent char[] array

StringBufferLengthCreateCharArray.java

package in.bench.resources.stringbuffer.methods;

public class StringBufferLengthCreateCharArray {

	public static void main(String[] args) {

		// StringBuffer object
		StringBuffer sbUrl = new StringBuffer("BenchResources.Net");

		// get length
		int length = sbUrl.length();

		// create character array object
		char[] chArray = new char[length];

		// iterate using for-loop & store into char[] array
		for (int index = 0; index < length ; index++) {

			// store into char[] array
			chArray[index] = sbUrl.charAt(index);
		}

		// print to console - this is demo purpose
		System.out.println("The converted char[] array is : " 
				+ String.valueOf(chArray));
	}
}

Output:

The converted char[] array is : BenchResources.Net

 

References:

https://docs.oracle.com/javase/tutorial/essential/concurrency/immutable.html
https://docs.oracle.com/javase/tutorial/essential/concurrency/imstrat.html
https://docs.oracle.com/javase/tutorial/java/data/strings.html
https://docs.oracle.com/javase/6/docs/api/java/lang/String.html
https://docs.oracle.com/javase/7/docs/api/java/lang/String.html
https://docs.oracle.com/javase/8/docs/api/java/lang/String.html
https://docs.oracle.com/javase/6/docs/api/java/lang/class-use/String.html
https://docs.oracle.com/javase/7/docs/api/java/lang/class-use/String.html
https://docs.oracle.com/javase/8/docs/api/java/lang/class-use/String.html
https://docs.oracle.com/javase/6/docs/api/java/lang/StringBuffer.html
https://docs.oracle.com/javase/7/docs/api/java/lang/StringBuffer.html
https://docs.oracle.com/javase/8/docs/api/java/lang/StringBuffer.html
https://docs.oracle.com/javase/6/docs/api/java/lang/StringBuilder.html
https://docs.oracle.com/javase/7/docs/api/java/lang/StringBuilder.html
https://docs.oracle.com/javase/8/docs/api/java/lang/StringBuilder.html

 

Happy Coding !!
Happy Learning !!

Java: StringBuffer replace() method
Java: StringBuffer lastIndexOf() method
  • Станислав Рымар

    Hm, thanks a lot its very usefull

  • Sayan Bhattacharyya

    the way that you present is very easy to understand… thanks a lot team
    could you please post on pagination

    • Thanks Sayan for your appreciation !!
      We will try to cover a post on pagination very soon.