Search Java Programs

Wednesday, January 20, 2010

Java String length Example

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
* @version 16-May-2008
* @author Rose India Team
*/


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

Website Design by Mayuri Multimedia