Monday, 7 May 2012

Java String length Example

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
String class length method example

String class length method example:- This example demonstrates the working .length method. this method returns total indexes that is what called length of the object .

Syntax:-length() 


Here is the code:-
/**
 * @(#) LengthString.java 
 * LengthString class demonstrates the working of .length() method of String class of lang package
 */

public class LengthString {
  public static void main(String args[]) {

    String heram = new String("Maryada"), prabhu = "purushottam", o_paalan_haari = "ram", bharat = heram
        + prabhu + o_paalan_haari,
    // .length method generates the number total of indexes available or in
    // use by the object value
    rawan = "lenth of String 'heram':   " + heram.length()
        + "\nlength of String 'prabhu':  " + prabhu.length()
        + "\nlength of String 'o_paalan_haari':   "
        + o_paalan_haari.length();

    System.out.println(rawan);

    int hanuman = bharat.length(), Sita_Mayya = rawan.length();
    System.out.println("length of hanuman:   " + hanuman);
    System.out.println("length of Sita_mayya:   " + Sita_Mayya);
    String baadshah = Integer.toString(hanuman), begum = Integer
        .toString(Sita_Mayya);

  }

}
Output of the program:-
----------------------------------------
lenth of String 'heram':   7
length of String 'prabhu':  11
length of String 'o_paalan_haari':   3
length of hanuman:   21
length of Sita_mayya:   98

No comments:

Post a Comment

Related Posts Plugin for WordPress, Blogger...