Java – StringBuilder length() method

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

1. StringBuilder’s length() method :

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

1.1 Method Signature:

public int length();

1.2 Returns:

  • Returns length i.e.; character count

2. Examples on length() method :

Generally, length() method of StringBuilder 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. dowhile-loop – used to keep boundary condition, while iterating
  5. To create equivalent char[] array

Let’s see examples for each listed above cases:

2.1 To get length or character count of StringBuilder :

StringBuilderLengthMethod.java

package in.bench.resources.stringbuilder.methods;

public class StringBuilderLengthMethod {

	public static void main(String[] args) {

		// StringBuilder object
		StringBuilder sbUrl = new StringBuilder("Google.com");

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

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

Output:

The length of StringBuilder 'Google.com' is 10

2.2 Boundary condition for for-loop while iterating :

StringBuilderLengthForLoop.java

package in.bench.resources.stringbuilder.methods;

public class StringBuilderLengthForLoop {

	public static void main(String[] args) {

		// StringBuilder object
		StringBuilder sbUrl = new StringBuilder("Oracle.com");

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

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

Output:

StringBuilder character at 0-position is : O
StringBuilder character at 1-position is : r
StringBuilder character at 2-position is : a
StringBuilder character at 3-position is : c
StringBuilder character at 4-position is : l
StringBuilder character at 5-position is : e
StringBuilder character at 6-position is : .
StringBuilder character at 7-position is : c
StringBuilder character at 8-position is : o
StringBuilder character at 9-position is : m

2.3 Boundary condition for while-loop while iterating

StringBuilderLengthWhileLoop.javac

package in.bench.resources.stringbuilder.methods;

public class StringBuilderLengthWhileLoop {

	public static void main(String[] args) {

		// StringBuilder object
		StringBuilder sbUrl = new StringBuilder("Amazon.com");

		// initialize index
		int index = 0;

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

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

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

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

Output:

StringBuilder character at 0-position is : A
StringBuilder character at 1-position is : m
StringBuilder character at 2-position is : a
StringBuilder character at 3-position is : z
StringBuilder character at 4-position is : o
StringBuilder character at 5-position is : n
StringBuilder character at 6-position is : .
StringBuilder character at 7-position is : c
StringBuilder character at 8-position is : o
StringBuilder character at 9-position is : m

2.4 Boundary condition for do-while-loop while iterating

StringBuilderLengthDoWhileLoop.java

package in.bench.resources.stringbuilder.methods;

public class StringBuilderLengthDoWhileLoop {

	public static void main(String[] args) {

		// StringBuilder object
		StringBuilder sbUrl = new StringBuilder("Apple.com");

		// initialize index
		int index = 0;

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

		// iterating using do-while-loop
		do {

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

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

Output:

StringBuilder character at 0-position is : A
StringBuilder character at 1-position is : p
StringBuilder character at 2-position is : p
StringBuilder character at 3-position is : l
StringBuilder character at 4-position is : e
StringBuilder character at 5-position is : .
StringBuilder character at 6-position is : c
StringBuilder character at 7-position is : o
StringBuilder character at 8-position is : m

Q) 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)

2.5 To create equivalent char[] array

StringBuilderLengthCreateCharArray.java

package in.bench.resources.stringbuilder.methods;

public class StringBuilderLengthCreateCharArray {

	public static void main(String[] args) {

		// StringBuilder object
		StringBuilder sbUrl = new StringBuilder(
				"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

Related Articles :

References :

Happy Coding !!
Happy Learning !!

Java - StringBuilder replace() method
Java - StringBuilder lastIndexOf() method