Get String length with String length() method
To find String length in java there is a method length().
This method is used to get the length of the string.
The signature of length method is:
1 | public int length() |
The length method returns the length of the string. it counts the number of Unicode in string.
str.length java Example
1 2 3 4 5 6 7 8 | public class MyClass { public static void main(String args[]) { String text="Hello How are you"; int length=text.length(); System.out.println("Length of string "+length); } } |
This is a very simple way to get the length of a string in java.
Output
1 | Length of string 17 |
Finding the length of unicode string in java
Here we are considering the Hindi language stored in String. We use length() to get the string length.
You can take any Unicode character to check length string length() works properly.
1 2 3 4 5 6 7 | public class StringLength { public static void main(String args[]) { String text = "उठो, जागो और तब तक मत रुको जब तक लक्ष्य की प्राप्ति ना हो जाये।"; int length = text.length(); System.out.println("Length of string " + length); } } |
Output
1 | Length of string 63 |
Finding the String length in java of unicode characters
Here we have stored a Unicode value in String and found its length.
1 2 3 4 5 6 7 8 | public class StringLength { public static void main(String args[]) { String text = "\u0041"; int length = text.length(); System.out.println("Length of string " + length); System.out.println("unicode character is " + text); } } |
Output
1 2 | Length of string 1 unicode character is A |
Using toCharArray() to find String length in java
toCharArray() is another method in the String class to convert String to a character array.
Signature of toCharArray() is
1 | public char[] toCharArray() |
1 2 3 4 5 6 7 8 | public class CharArrayLength { public static void main(String[] args) { String text = "When our false perception is corrected, misery ends also"; char[] charArray = text.toCharArray(); int length = charArray.length; System.out.println("Length of text is " + length); } } |
In the above program String is converted to a character array using toCharArray().
Character array has a field length. This length property finds the length of the character array.
Output
1 | Length of text is 56 |
Using Function to find the length
In the above program, you find the length of the Java String in the main method.
Here we have created two static methods length() and length1().
these two methods are accepting string and getting its length.
You can also use command line argument to get the string at run time.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 | public class StringLenghtForLoop { public static int length(String s) { return s.length(); } public static int length1(String s) { return s.toCharArray().length; } public static void main(String[] args) { String text = "Hello How are you"; int length = length(text); System.out.println("String length is " + length); int length1 = length1(text); System.out.println("String length is " + length1); } } |
Using Recursion to find length
Here we are using recursion to find the length of the string.
When a function is called it is checking the length of the string if the string is empty then return zero else calls again itself by adding one to the return value.
On each recursive call, we are eliminating the first character from the string.
Create your own string length method in java
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 | public class StringLenght { public static int length(String s) { if (s.isEmpty()) { return 0; } else { return length(s.substring(1))+1; } } public static void main(String[] args) { String text = "Hello How are you"; int length = length(text); System.out.println("String length is " + length); } } |
Output
1 | String length is 17 |
Hope you learn and understand how to get the length of a string as we discussed different ways to find string length.
substring length java
Substring length can also be found the same was as any string length.
call length method on substring subString.length()
Q: how to calculate length of a string in java
Ans: Java String class has an inbuilt method length(). From this method, we can get the length of a string.
Another way is by manually counting the number of characters in the string (using loops).
Keep Reading other topics.