Java Program to Find Length of a String

  • Java program to find length of a sting using length method.

In this java program, to find the length of a string we will use length() method. This method returns the length of this string which is equal to the number of characters in the string.

For Example,
Length = 15

Java program to find length of string using length method

In this java program, we first ask user to enter a string and then store it in String object "str". Then we print the length of input string str by calling length() method.


import java.util.Scanner;

 * Java Program to Find Length of a String
public class StringLength {
    public static void main(String args[]) {
        String str;
        int length;
        Scanner scanner = new Scanner(;

        System.out.println("Enter a String");
        str = scanner.nextLine();
        // Printing length of input string

        System.out.print("Length of Input String : " + str.length());
Enter a String
Length of Input String : 15
Enter a String
abcd  terd
Length of Input String : 10