Java String length() method explained
It is a predefined function which is present in the Java.lang.String package. It is used to find and return the length of a given string have a look on the syntax of this method-
Syntax of length() method
public int length() {}
Example 01 of length() method
public class Length {
public static void main(String[] args)
{
String s="Love";
int l=s.length();
System.out.println(l);
}
}
output:
4
Example 02 of length() method
Problem statement- "Take a string from user and and output the length of the string entered by the user repeatedly"-
Source code-
import java.io.*;
public class Length {
public static void main(String[] args) throws IOException
{
BufferedReader b = new BufferedReader(new InputStreamReader(System.in));
while(true)
{
System.out.println("enter a string");
String s = b.readLine();
System.out.println(s.length());
}
}
}
output:
enter a string
Captain
7
enter a string
Jack
4
enter a string
Sparrow
7
enter a string
Hello, visitor your comments are so helpful for us so do not hesitate just write the comment we read all your comments so don't think your comment goes waste.