ZMedia Purwodadi

Java Strings length() method explained | technical-arbaab

Table of Contents

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  




Arbaab khan
Arbaab khan FEAR has two meanings "forget everything and run" "Face everything and rise" The choice is yours Welcome u in our tech blog

إرسال تعليق