1public class Sample_String {
2 public static void main(String[] args) {
3 //declare the String as an object S1 S2
4 String S1 = "Hello Java String Method";
5 String S2 = "RockStar";
6
7 //length() method of String returns the length of a String S1.
8 int length = S1.length();
9 System.out.println("Length of a String is: " + length);
10 //8 Length of a String RockStar
11 System.out.println("Length of a String is: " + S2.length());
12 }
13}
14
1// String length() method example
2public class StringLengthJava
3{
4 public static void main(String[] args)
5 {
6 String str1 = "flowerbrackets";
7 String str2 = "helloworld";
8 System.out.println("string length : " + str1.length());
9 System.out.println("string length : " + str2.length());
10 }
11}
1length() : In String class we have length() method which is used
2to return the number of characters in string.
3Ex : String str = “Hello World”;
4System.out.println(str.length());
5Str.length() will return 11 characters including space.
6
7length : we have length instance variable in arrays which will
8return the number of values or objects in array.
9For example :
10String days[]={” Sun”,”Mon”,”wed”,”thu”,”fri”,”sat”};
11Will return 6 since the number of values in days array is 6