Pin It

How to know the length of a string and its contents in Java ?.

Program to demonstrate how to know the length of a string and its contents in Java .


package com.hubberspot.example;


public class StringLength {

   public static void main(String[] args) {

 // Create a simple String
 String str = "A simple String";
 // Get the length of string using 
 // length() method in String
 int length = str.length();

 // printing the length of string over console
 System.out.println("The length of String object above is : " + length);

 System.out.println("Let's browse the content of string using length : ");

 // looping over the integer length one by one
 for (int i = 0; i < length; i++) {
  // displaying character along with index in a string
  System.out.println("character at " + i + " : " + 
    str.charAt(i) + "  ");

 }
   }
}


Output of the program : 
 
Please Give Us Your 1 Minute In Sharing This Post!
SOCIALIZE IT →
FOLLOW US →
SHARE IT →
Related Posts Plugin for WordPress, Blogger...

Post your related queries below ...

comments powered by Disqus
Powered by Blogger.
 
© 2012 Learn Java by Examples Template by Hubber Spot