Latest Tutorials| Questions and Answers|Ask Questions?|Site Map



Home Java Beginners String length example

 
 

Share on Google+Share on Google+

String length example

Advertisement
In this section, you will learn how to get the length of given string.

String length example

     

In this section, you will learn how to get the length of given string. The lang package of java provides the facility for getting the length of string. You will see in the following program for getting the length of string by using the length() method that returns integer type value of any given string. After running this program takes any string in command line and calculates its length.

Description of code:

length():
This is the method that returns the length of given string in integer type.

Here is the code of program:

import java.lang.*;
import java.io.*;

public class StringLength{
  public static void main(String[] argsthrows IOException{
  System.out.println("String lenght example!");
  BufferedReader bf = 
 
new BufferedReader(new InputStreamReader(System.in));
  System.out.println("Please enter string:");
  String str = bf.readLine();
  int len = str.length();
  System.out.println("String lenght : " + len);
  }
}

Download this example.

Output of program:

C:\vinod\Math_package>javac StringLength.java

C:\vinod\Math_package>java StringLength
String lenght example!
Please enter string:
RoseIndia.net
String lenght : 13
Advertisements

If you enjoyed this post then why not add us on Google+? Add us to your Circles



Liked it!  Share this Tutorial


Follow us on Twitter, or add us on Facebook or Google Plus to keep you updated with the recent trends of Java and other open source platforms.

Posted on: June 8, 2007

Related Tutorials

Discuss: String length example   View All Comments

Post your Comment


Your Name (*) :
Your Email :
Subject (*):
Your Comment (*):
  Reload Image
 
 
Comments:3
aravind
June 30, 2011
length of the null string

String str=null; System.out.print(str.length());
Mike_S
September 5, 2011
spelling

lenght >>> length
bikram
October 1, 2011
enquary

I am getting the following error messages in finding of sting length..Can u Solve my problem---- error: Class names, 'StringLength1', are only accepted if annotation processing is explicitly requested 1 error
DMCA.com