String length()

We have learned about string in java and why is it immutable in java. In this topic we will see the string length() method and how does string length() in java work? To operate a string, we need to find the string length of string because if we exceed the length of string the JVM throws a StringIndexOutOfBoundExcetion.

Here is the table content of the article will we will cover this topic.
1. What is String length in java?
2. Syntax of java value() method
3. How does the value() method work?
4. Count the length without whitespace

What is String length in java?

As we know String is an array of characters and each character stored in a particular index. To find the length of the string, the String class provides a length() method. The string length method finds the number of characters present in the String. The length() method counts each character even it is white space.

Syntax of java value() method

string length in java

access modifier: The access modifier of the value() method is public. So, can be accessible from everywhere.
return type: The return type of value() method is int.
Parameter: It doesn’t take any parameter.

public class StringExample
{
	public static void main(String[] args) 
	{
		String str1 = "JavaGoal website";
		String str2 = " JavaGoal website  ";
		String str3 = "  ";
		String str4 = "";
		
		System.out.println("Length of str1:"+str1.length());
		System.out.println("Length of str2:"+str2.length());
		System.out.println("Length of str3: "+str3.length());
		System.out.println("Length of str4: "+str4.length());
	}
}

Output: Length of str1:16
Length of str2:19
Length of str3: 2
Length of str4: 0

How does the value() method work?

The string length is calculated based on the character presented in the string. The length() method counts all character that presents in string even it is blank space. The first character stores on index 0 and the last one stores at length-1. Suppose we have a String “ JavaGoal  ” and we want to find the length of the string. Then we will invoke the length() method of string class.

string length in java
public class StringExample
{
	public static void main(String[] args) 
	{		
		String str1 = " JavaGoal website  ";
		System.out.println("Length of str1:"+str1.length());
	}
}

Output: Length of str1:19

Count the length without whitespace

If you want to count the length of string but you don’t want to include whitespaces, then we should use the trim() method before calling the length() method. The trim() method removes the whitespaces from both ends.

public class StringExample
{
	public static void main(String[] args) 
	{		
		String str1 = " JavaGoal website  ";
		String str2 = "  ";
			
		System.out.println("Length of str1:"+str1.length());
		System.out.println("Length of str1:"+str1.trim().length());
		System.out.println("Length of str2:"+str2.length());
		System.out.println("Length of str2: "+str2.trim().length());
	}
}

Output: Length of str1:19
Length of str1:16
Length of str2:2
Length of str2:0

Leave a Comment