Deva Point

deva point

Our Latest Programming Tutorial

Easy To Learning

Java Sting Length

The Java String class length() method finds the length of a string. The length of the Java string is the same as the Unicode code units of the string. This method calculates how long a string is that is greater than the amount of 16 bit Unicode characters contained in the string.

string.length(): This functioncan be described as a method that is suitable for string objects. The length() method provides how many characters present inside the string. This length() method can be used for strings, but not for arrays. length() procedure as the catch() procedure, which offers a value from that class field back to users. Its internal code clearly demonstrates the way that this length() method returns the value of the variable length.

Java Sting Length with example

This function can be used to determine the length of strings in Java. The method for length of string gives how many characters that are written into the String.

Example 1:  Write a program to find out Sting Length in Java

Java String length() method example

FileName: LengthExample.java

public class LengthExample

public static void main(String args[])

String s1=”Good”; 

String s2=”Morning”; 

System.out.println(“string length is: “+s1.length());//4 is the length of javatpoint string 

System.out.println(“string length is: “+s2.length());//7 is the length of python string 

}

Output:

string length is: 4

string length is: 7

Example 2: Program to print java sting length()

import java.io.*;

public class Test

{

   public static void main(String args[])

{

      String Str1 = new String(“Welcome to Devapoint”);

      String Str2 = new String(“Best Tutorial” );

      System.out.print(“String Length :” );

      System.out.println(Str1.length());

      System.out.print(“String Length :” );

      System.out.println(Str2.length());

   }

}

This will produce the following result −

Output

String Length :20

String Length :13

Examples: 3 Program to get the length of String[] in Java using length variable

String[].length: length is a final variable applicable for arrays. With the help of length variable, we can obtain the size of the array.

int size = String[].length;

// length can be used for String[]

Below is the illustration of how to get the length of String[] in Java using length variable:

// in Java using length variable

public class Test {

    public static void main(String[] args)

    {

        // Here str is the array name of String type.

        String[] str = { “Devapoint”, “is”, “the”,”Best”, “Tutorial”};

        System.out.println(str.length);

    }

}

Output: 5

Published
Categorized as Java

Leave a comment

Your email address will not be published. Required fields are marked *