1String data = "Hello, World!";
2int nameLength = data.length(); // 13
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}