Search Java Programs

Showing posts with label length(). Show all posts
Showing posts with label length(). Show all posts

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

length() Method In Java

In this section, you will learn how to use length() method of the String class. We are going to using length() method. These method are illustration as follow here:

Description of program:

Here, we will know that how many character of length in String. First of all, we have to define class named "StringLength". Inside of class we have to define method of class in main class. After that we create one method which returns the integer type values. So we are using to length() method of the String class for the purpose.

length() method : This method return the length of this string as an integer value.

Here is the the code of this program:

import java.io.*;

class StringLength{
public static void main(String[] args){
try{
BufferedReader object=new BufferedReader (new InputStreamReader(System.in));
System.out.println("Eneter string value:");
String s=object.readLine();
int len=s.length();
System.out.println(len);
}
catch(Exception e){}
}
}
Output this program:
C:\java_work>javac StringLength.java
C:\java_work>java StringLength
Eneter string value:
amar
4

Website Design by Mayuri Multimedia