Here is a 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;
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