Skip to main content

HOW TO FIND LENGTH OF A STRING IN JAVA

Simple example to use string in different ways.

Syntax :

Some_string_variable_name.length()

Example :

  1. public class StringDemo { 
  2.    public static void main(String args[]) {
  3.       String palindrome = "Dot saw I was Tod";
  4.       int len = palindrome.length();
  5.       System.out.println( "String Length is : " + len );
  6.    }
  7. } 

Description :

Step 1:Starting of class
Step 2:Start of main function.
Step 3:Define a string "palindrome"
Step 4:Defining and initializing a variable "len". Here the length(number of characters) of the string "palindrome" is copied to variable "len" which is of integer type.
Step 5:Display content of variable "len" to console.

Output:

String Length is : 17