This Tutorial Explains all about the Java String length() Method along with Multiple Programming Examples & FAQs to help you Understand the Concept:
Moreover, we will be covering the different scenarios related to the String Java length() method. Frequently asked questions related to the Java String length() method will also be a part of this tutorial.
Upon going through this tutorial, you will be able to calculate the length of a String and use it in many different cases or scenarios. This method works well with the other Java String methods.
=> Check Out The Perfect Java Training Guide Here
Table of Contents:
Java String Length
Length of a String is nothing but the number of characters that it contains. Java has an inbuilt method called length() to find the number of characters of any String.
Syntax:
The syntax is given as
int length();
where length() is a method to find the number of characters and returns the result as an integer.
Finding The Length Of A String
In this example, we will be covering the simplest form of Java String length() method. We will initialize a String with some value and then we will calculate the length.
public class length { public static void main(String[] args) { // Initialized a String variable String str = "Testing"; // Initialized a count variable which will store the length int count = str.length(); // Printed the count variable or the length of String. System.out.println("The String has " +count +" characters"); } }
Output:
Finding The Length Of A Character Array
In this example, we have created a character array “chars” and then merged those characters in a String Variable “str” and then printed the variable and the length.
public class length { public static void main(String[] args) { // Initialized a character array char chars[] = { 'T', 'e', 's', 't', 'i', 'n', 'g' }; // Initialized a String variable str with chars characters String str = new String(chars); // Printed the String variable System.out.println(str + " has "); // Printed the length of the String Variable System.out.println(str.length()+ " characters"); } }
Output:
Java String Length Scenarios
Scenario 1: Finding the length of a String which has whitespace.
Explanation: In this scenario, we will find the length of a String that has more than one word or substring and they are separated by whitespace.
Here, we have initialized two String variables with single and double whitespaces which will be treated as a character. Then, we initialized two count variables which will store the length.
Finally, we have printed the count variables.
public class length { public static void main(String[] args) { // Initialized a String variable with a single whitespace String str1 = "This is"; // Initialized another String variable with two whitespace String str2 = "Software Testing Help"; /* * Initialized a count1 variable which will store the length of the first String. */ int count1 = str1.length(); /* * Initialized a count2 variable which will store the length of the second String. */ int count2 = str2.length(); // Printed the count1 variable. System.out.println("The First String has " + count1 + " characters"); // Printed the count2 variable. System.out.println("The Second String has " + count2 + " characters"); } }
Output:
Scenario 2: Finding the length of a String which has special characters.
Explanation: Here, we are going to initialize a String with special characters and will try to get the length of the String.
public class length { public static void main(String[] args) { // Initialized a String variable with special characters String str = "P@!.90$%"; /* * Initialized a count variable which will store the length of the String. */ int count = str.length(); // Printed the count variable. System.out.println("The String has " + count + " characters"); } }
Output:
Frequently Asked Questions
Q #1) What does String length() do in Java?
Answer: It returns the number of characters of a String. The index in Java starts from 0 and continues till the nth character the String.
The length would be the index of the last element + 1.
For Example:
String str = “Hello World”
Here, H is at index[0], e is at index [1], and so on.
The last element is d which is at index[10]. So, the total length is 11.
Q #2) What is a Character in Java?
Answer: Character is nothing but the letter that combines together to form a String. Java also considers whitespaces as a character. When you are going to compute the length of a String which has whitespace, special characters, etc., then they will be treated as a character.
Every single character has size = 1.
Q #3) How to create a String of specified size in Java?
Answer: In this program, we have created two constants. The first constant is the character which will repeatedly occur in the String and the second constant is the number of times it will occur. Then we have stored all the elements of the character array into String.
Later, we replaced all the NULL characters with the first constant character. Finally, it returned the String and printed the value.
public class length { // Initialized a constant character which will repeatedly occur static final char chars = '$'; // Specied a constant length limit as 5 static final int StrLen = 5; public static void main(String[] args) { // printing the return value of the create method System.out.println(create()); } public static String create(){ //created a new String from the character array String str = new String(new char[StrLen]); //replaced all NULL chars '\0' with specified character $ str = str.replace('\0', chars); return str; } }
Output:
Q #4) How to change the length of the String?
Answer: In the below program, we have changed the length of the String by replacing the substring with a blank.
We have taken an input String and then printed the String and the length of the String. Then, we have replaced the substring of the main String with a blank value.
Again, we have printed the String and the length of the String.
public class length { public static void main(String[] args) { // Initialized a String variable String str = "Software Test"; // Printed the String and the length System.out.println(str + " has " +str.length()+ " characters"); // Replaced the substring Test with a blank value str = str.replace(" Test", ""); // Printed the String and the length System.out.println(str + " has " +str.length()+ " characters"); } }
Output:
Q #5) What is the Array length in Java? How is it different from String length()?
Answer: In Array, the length is a variable that is used to get the length of an Array. All we have to do is put Array.length and it will give you the length.
In String, the length() is a method that is used to get the length of a String. We get the length by putting String.length()
In the below program, let’s see how it works.
public class length { public static void main(String[] args) { // Specified the length of an Array as 4. int[] arr = new int[4]; // returned the length of an Array System.out.println("Array length is " + arr.length); String str = "Saket"; // returned the length of the String System.out.println("String length() is " + str.length()); } }
Output:
Conclusion
In this tutorial, we have understood the Java String length() method in detail. This is the most basic String method that is used in collaboration with other String methods to achieve the desired result.
For better understanding, we have given different cases or scenarios and FAQs related to the String Length. Although the functional area of this method is small, the application area is as big as any other method.
This is the most simple and basic method of the String Class.
=> Visit Here For The Exclusive Java Training Tutorial Series.