Java – String length() method

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

1. String’s length() method:

  • This String method is used to get length of the string
  • sequence of characters
  • number of characters

1.1 Method Signature:

public int length();

1.2 Returns:

  • The length of the sequence of characters represented by this object

2. Examples on length() method:

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

  1. To get length of any String
  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 cases above,

2.1 Get length of any string

StringLengthMethod.java

package in.bench.resources.string.methods;

public class StringLengthMethod {

	public static void main(String[] args) {

		// sample string
		String url = "BenchResources.Net";

		// to check length of the string
		int len = url.length();

		// print to console
		System.out.println("The length of string '"
				+ url + "' is " + len);
	}
}

Output:

The length of string 'BenchResources.Net' is 18

2.2 Boundary condition for for-loop while iterating

StringLengthForLoop.java

package in.bench.resources.string.methods;

public class StringLengthForLoop {

	public static void main(String[] args) {

		// sample string
		String url = "BenchResources.Net";

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

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

Output:

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

2.3 Boundary condition for while-loop while iterating

StringLengthWhileLoop.java

package in.bench.resources.string.methods;

public class StringLengthWhileLoop {

	public static void main(String[] args) {

		// sample string
		String url = "BenchResources.Net";

		// initialize index
		int index = 0;

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

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

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

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

Output:

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

2.4 Boundary condition for do-while-loop while iterating

StringLengthDoWhileLoop.java

package in.bench.resources.string.methods;

public class StringLengthDoWhileLoop {

	public static void main(String[] args) {

		// sample string
		String url = "BenchResources.Net";

		// initialize index
		int index = 0;

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

		// iterating using do-while-loop
		do {

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

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

Output:

The character at 0-position is : B
The character at 1-position is : e
The character at 2-position is : n
The character at 3-position is : c
The character at 4-position is : h
The character at 5-position is : R
The character at 6-position is : e
The character at 7-position is : s
The character at 8-position is : o
The character at 9-position is : u
The character at 10-position is : r
The character at 11-position is : c
The character at 12-position is : e
The character at 13-position is : s
The character at 14-position is : .
The character at 15-position is : N
The character at 16-position is : e
The 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)

2.5 To create equivalent char[] array

StringLengthCreateCharArray.java

package in.bench.resources.string.methods;

public class StringLengthCreateCharArray {

	public static void main(String[] args) {

		// sample string
		String url = "BenchResources.Net";

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

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

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

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

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

Output:

The converted char[] array is : BenchResources.Net

Related Articles:

References:

Happy Coding !!
Happy Learning !!

Java - String matches() method
Java - String lastIndexOf() method