Saturday 11 August 2012

Java String Length Example


This example shows how to get a length of a given String object using length() method of the String class.



public class StringLengthExample {

 public static void main(String[] args) {

  // declare the String object

  String str = "Hello World";

  // length() method of String returns the
  // length of a String.

  int length = str.length();

  System.out.println("Length of a String is : " + length);
 }
}


The output is:

Length of a String is : 11

No comments:

Post a Comment