How to Find the Length of a String in Java

While dealing with bulk data in Java, there can be instances when one needs to modify a particular value considering its length. More specifically, managing or updating the string data. In such situations, finding the length of a string in Java is assistive in analyzing the contained data and managing the resources effectively.

This write-up will elaborate on the approaches to compute the string length in Java.

How to Find/Compute the Length of a String Using Java?

The length of a Java string can be computed via the “length()” method. This method calculates the number of characters in the provided string.


int length()

In the above syntax, “int” indicates that the return type of this method is an integer.

Example 1: Compute the Length of a Given String in Java

In this example, the initialized string value can be computed for the length:

String givenString = "JavaString";
int result = givenString.length();
System.out.print("The length of the string is: ");

In the above code part:

  • Firstly initialize the specified string value.
  • In the next step, apply the “length()” to compute the length of the associated string.
  • Lastly, display the resultant string length:


In the above output, it can be seen that the length of the corresponding string is displayed as “10”.

Example 2: Compute the Length of a String With Removed Whitespaces

The “trim()” method in Java can be utilized to omit all the contained whitespaces in a string. In this particular example, this method can be implemented combined with the discussed “length()” method to trim the contained whitespaces in the given string and display the resultant string length.


String trim()

In the above-given syntax, “String” refers to the return type being a string.

Let’s move on to the following example to clarify the discussed concept:

String givenString = " JavaString ";
System.out.print("The actual string length is: ");
int result= givenString.trim().length();
System.out.print("The string length with removed "
+ "whitespaces is: ");

According to the above code:

  • First of all, likewise specify the stated string value containing the padded white spaces at the beginning and end.
  • In the next step, apply the “length()” method to return the string length and display it.
  • Note that this method computes the appended whitespaces with the string.
  • After that, apply the combined “trim()” and “length()” methods to omit the accumulated white spaces in a string and then calculate the string length.
  • Finally, display the resultant length of the string with removed whitespaces:


In this particular output, the difference between the actual string length and the string length with omitted whitespaces can be observed.


The “length()” method can be utilized to compute the length of a string in Java. The former example simply computes the string length. The latter example firstly omits the padded white spaces from the start and end in a string and then displays the corresponding string length. This blog discussed the approaches to find the string length in Java.

About the author

Umar Hassan

I am a Front-End Web Developer. Being a technical author, I try to learn new things and adapt with them every day. I am passionate to write about evolving software tools and technologies and make it understandable for the end-user.