Java String length() method
Java String length() method returns length of the characters in a String.
Syntax:
public int length()
Returns
It returns length of the characters
Java String length() method example 1
public class JavaStringLengthEx1
{
public static void main(String args[])
{
String str1="";
String str2="Java World";
String str3="tutorialsandexample";
System.out.println("string length is: "+str1.length());
System.out.println("string length is: "+str2.length());
System.out.println("string length is: "+str3.length());
}
}
Output:
string length is: 0 string length is: 10 string length is: 19
Java String length() method example 2
public class JavaStringLengthEx2
{
public static void main(String[] args)
{
String aboutMe = "Hello I am Roy NK";
if(aboutMe.length()>0) {
System.out.println("Its looking Good and length of About Me is: "+aboutMe.length());
}
aboutMe = "";
if(aboutMe.length()==0) {
System.out.println("About Me must not be empty: "+aboutMe.length());
}
}
}
Output:
Its looking Good and length of About Me is: 17 About Me must not be empty: 0
