Find the length of the string in Java

length() method of String class can be used to find the length of the String in Java.

It returns the length of the string in integer value.


Example 1

class StringTest{

public static void main(String args[]){

String str="Friend indeed is a friend in need";

int length=str.length();

System.out.println("Length of the string str is "+length);

}

}

Output 1

Length of the string str is 33


Example 2

class StringTest{

public static void main(String args[]){

String str="Hi! How are you ?";

int length=str.length();

System.out.println("Length of the string str is "+length);

}

}

Output 2

Length of the string str is 17