StringBuffer example

StringBuffer example
 package com.ram;

public class StringBufferExample {
 public static void main(String[] args) {
  StringBuffer sBuffer = new StringBuffer();
  sBuffer.append("If");
  sBuffer.append(" you");
  sBuffer.append(" can");
  sBuffer.append(" dream");
  sBuffer.append(" it,");
  sBuffer.append(" you");
  sBuffer.append(" can");
  sBuffer.append(" do");
  sBuffer.append(" it");
  
  System.out.println("Complete sentence: \n"+sBuffer);
  
  //To know the length of StringBuffer
  System.out.println("Sentence length = "+sBuffer.length());
  
  //To get a character at a particular position
  System.out.println("Character at position 5 is :: "+sBuffer.charAt(5));
  
  //To get a part of string
  System.out.println(sBuffer.substring(21, 34));
  
  //To reverse StringBuffer
  System.out.println("Reverse  = "+sBuffer.reverse());
  
 }
}
 
 
Execute StringBufferExample class and you get the below output:
Complete sentence:
If you can dream it, you can do it
Sentence length = 34
Character at position 5 is :: u
you can do it
Reverse = ti od nac uoy ,ti maerd nac uoy fI

No comments:

Post a Comment