How to Get the Length of the String in Java?

You are currently viewing How to Get the Length of the String in Java?

How to get the length of the string in java?

import java.util.Scanner;

public class ReturnLength
{
	public static void main(String[] args)
	{
		// create object of string and scanner class.
		String text;
		Scanner SC=new Scanner(System.in);

		// enter the string here.
		System.out.print("Enter the string : ");
		text=SC.nextLine();

		// find string length
		int length = text.length();
		// print length.
		System.out.println("Length of the given string " + text+ " is : " +length );
	}
}

Output

First run:
Enter the string : Include Help
Length of the given string Include Help is : 12

Second run:
Enter the string : We are programmers.
Length of the given string We are programmers. is : 19

Ranjith

Hi, I'm Manoj a full-time Blogger, YouTuber, Affiliate Marketer, & founder of Coding Diksha. Here, I post about programming to help developers.

Leave a Reply