Quick Reach
The String length method of Java
The length method of the String class returns total length of the specified string or the total number of characters in the given string.
Length method syntax
The syntax of the Java string length method is:
Strex.length()
Where the Strex is a String class object.
Example of using string length method
In the following example of Java length method of the String class, we have created two string objects by two different methods. One is simply declaring a variable with the String keyword. While the second variable is created by using the new keyword.
After that, we have assigned values, that are two strings to these variable. Finally, the length method of Strings is used to get the length of two strings and printed on the screen by using the System.out.println statement of Java.
Experience this online
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
|
public class string_example {
public static void main(String []args) {
String Strex = “Strings are supported in java by using String class”; // creating simple
String strex1 = new String(); //creating with new keyword
strex1 = “Java comes up with string class”;
System.out.println(“String length java=” + Strex.length());
System.out.println(“String length java=” + strex1.length());
}
}
|
The output will be:
String length java=51
String length java=31
Also see Java string
Leave A Comment?