Write a program to get the length of a given string


The program defines a public class named "String_Length" with a main method that takes no arguments. The main method declares a String variable str with the value "Java Exercise".

Then, the program uses the length() method of the String class to calculate the length of str and stores the result in the integer variable len.

Finally, the program prints out the original String str and the resulting length len using the System.out.println method.

Source Code

public class String_Length
{
	public static void main(String[] args)
	{  
		String str = "Java Exercise";
		int len = str.length();
		System.out.println("Given String : "+str);
		System.out.println("String Length : "+len);
	}
}

Output

Given String : Java Exercise
String Length : 13

Example Programs