Java String length() Method
The length() method is used to return the length of a string.
The length of an empty string returns 0.
Syntax
public int length()
Parameters
- None
Return Value
Returns the length of the string.
Example
The following example demonstrates the use of the length() method:
Example
public class Test {
public static void main(String args[]) {
String Str1 = new String("www.example.com");
String Str2 = new String("example" );
System.out.print("String Str1 length :");
System.out.println(Str1.length());
System.out.print("String Str2 length :");
System.out.println(Str2.length());
}
}
public static void main(String args[]) {
String Str1 = new String("www.example.com");
String Str2 = new String("example" );
System.out.print("String Str1 length :");
System.out.println(Str1.length());
System.out.print("String Str2 length :");
System.out.println(Str2.length());
}
}
The output of the above program is:
字符串 Str1 长度 :14 字符串 Str2 长度 :6
Other Extensions
Java String Class