Menu
Topics Index
...
`

String Length - Java Example Program


String Length
class StringLengthTest
{
    public static void main(String arg[])
    {
        String place = "Hyderabad";
        int length = place.length(); // LINE A
        System.out.println("String Length of " + place + " is : " + length);
        
        String welcome = "Welcome to Merit Campus";
        for (int i = 0; i < welcome.length(); i++) // LINE B
        {
            System.out.print(welcome.charAt(i));
        }    
    }
}
OUTPUT

String Length of Hyderabad is : 9
Welcome to Merit Campus

DESCRIPTION

At LINE A, string length of "Hyderabad" is calculated by length method is stored in length variable and displayed.
At LINE B, for loop executes 23 times as the length of "Welcome to Merit Campus" string object is 23 and displays every character from beginning to end.

THINGS TO TRY
  • Apply length method for different strings such as "12345", "Hello World", "Ramu\t\nRavi", "15-08-1947 00:25:30" etc.,
  • Apply length method in other control statements as used in for loop at LINE B.

© meritcampus 2019

All Rights Reserved.

Open In App