Find length of string java – Java Program to Find Length of a String

Find length of string java: If you are new to Java and want to learn the java coding skills too fast. Try practicing the core java programs with the help of the Java basic programs list available.

Java Program to Find Length of a String

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

How to find length of a string in java: 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,
Input String : TECHCRASHCOURSE
Length = 15

Java program to find length of string using length method

Java find length of string: 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.
Java program to find length of string using length method

package com.tcc.java.programs;
 
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.in);
 
        System.out.println("Enter a String");
        str = scanner.nextLine();
        // Printing length of input string
 
        System.out.print("Length of Input String : " + str.length());
    }
}

Output

Enter a String
TechCrashCourse
Length of Input String : 15
Enter a String
abcd  terd
Length of Input String : 10