Register Login

Java String length() method with Example Program

Updated Jan 08, 2020

What is Java String Length Function?

The function String Length() is used to find the length of a string in Java. This string Length() returns the number of characters present in any string which is equal to the number of 16-bit Unicode characters in the string.

Note: 

  • If the input string is empty, then the function will return 0
  • The function will count space as a character

Syntax

public int length()

Parameters

The string length() function do not take any parameter

Return Value

This string length() function returns an integer value implying the number of characters in the string.

Example

Program to Find String Length in Java

//Java program to find the length of string

//Import the java util package

import java.util.Scanner;

//Main Class of the program
public class Main
{
    //Main Method of the program
    public static void main(String[] args) {
        //Creating the Scanner Class object
        Scanner scan = new Scanner(System.in);
        //Giving a hint to the user,What to enter
        System.out.print("Enter the string : ");
        //Taking input from user
        String string_1 = scan.nextLine();
        //Finding the length of string
        System.out.println("The string " + string_1 + " has length of "+string_1.length() + " Character");

    }
}

Output

Enter the string : STechies
The string STechies has length of 8 Character

 


×