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,
Input String : TECHCRASHCOURSE
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.

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

Recommended Posts
Java Program to Reverse a String
Java Program to Concatenate Two Strings
Java Program to Copy a String
Java Program to Delete All Spaces from a String
Java Program to Find Count of Each Character in a String
Java Program to Check Two Strings are Anagram or Not
Java Program to Delete all Vowel Characters from String
Java Program to Delete a Word from Sentence
Java Program to Check Whether two Strings are Equal or Not
Java Program to Reverse Sequence of Words of a Sentence
Java Program to Count Words in a Sentence
All Java Programs