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 :
