Home | Ajax | BioInformatics | Dojo | EAI | EJB | Hibernate | J2ME | Java | Java Glossary | Java Servlets | JavaScript | Jboss | JDBC | JDO | Jmeter | JSF | JSP | JUnit | Maven | MySQL | Spring Framework | SQL | Struts | Technology | WAP | Web Services | XML


 
  
 
Programming Tutorials: Ajax | Articles | JSP | Bioinformatics | Database | Free Books | Hibernate | J2EE | J2ME | Java | JavaScript | JDBC | JMS | Linux | MS Technology | PHP | RMI | Web-Services | Servlets | Struts | UML
 

 
Facing Programming Problem?
Ask Questions?, Browse Latest Questions, Question-Answer Guidelines
Java
  JDO Tutorials
  EAI Articles
  Struts Tutorials
  Java Tutorials
  Java Certification
  Java Applet
Questions
Comments

Java - StringBuffer class in Java

                         

In this example you will learn about StringBuffer class. This example explains how you can use functions provided by the StringBuffer class like append, insert, reverse, setCharAt, charAt, length, deleteCharAt, substring, delete, capacity etc. to manipulate the string operation in your program.

The StringBuffer class is used to represent characters that can be modified. This is simply used for concatenation or manipulation of the strings. 

StringBuffer is mainly used for the dynamic string concatenation which enhances the performance. A string buffer implements a mutable sequence of characters. A string buffer is like a String, but can be modified. At any point in time it contains some particular sequence of characters, but the length and content of the sequence can be changed through certain method calls. There are some functions used in the given example. All the functions have been explained below with example :

append()

This is the append() function used for the concatenate the string in string buffer. This is better to use for dynamic string concatenation. This function works like a simple string concatenation such as : String str = str + "added string";.

insert()

This is the insert() function used to insert any string or character at the specified position in the given string.

reverse()

This is the reverse() function used to reverse the string present in string buffer.

setCharAt()

This is the setCharAt() function which is used to set the specified character in buffered string at the specified position of the string in which you have to set the given character.

charAt()

This is the charAt() function which is used to get the character at the specified position of the given string.

substring()

This is the substring() function which is used to get the sub string from the buffered string from the initial position to end position (these are fixed by you in the program).

deleteCharAt()

This is the deleteCharAt() function which is used to delete the specific character from the buffered string by mentioning that's position in the string.

length()

This is the length() function is used to finding the length of the buffered string.

delete()

This is the delete() function is used to delete multiple character at once from n position to m position (n and m are will be fixed by you.) in the buffered string.

capacity()

This is the capacity() function is used to know about the current characters kept which is displayed like : number of characters + 6.

Code for the program : 

import java.io.*;

public class stringBuffer{
  public static void main(String[] args) throws Exception{
    BufferedReader in = new BufferedReader(new InputStreamReader(System.in));
    String str;
 
   try{
      System.out.print("Enter your name: ");
      str = in.readLine();
      str += ", This is the example of SringBuffer class and it's functions.";

      //Create a object of StringBuffer class
      StringBuffer strbuf = new StringBuffer();
      strbuf.append(str);
      System.out.println(strbuf);
      strbuf.delete(0,str.length());
 
     
      //append()
      strbuf.append("Hello");
      strbuf.append("World");              //print HelloWorld
      System.out.println(strbuf);
      
      //insert()
      strbuf.insert(5,"_Java ");            //print Hello_Java World
      System.out.println(strbuf);
  
    
      //reverse()
      strbuf.reverse();
      System.out.print("Reversed string : ");
      System.out.println(strbuf);            //print dlroW avaJ_olleH
      strbuf.reverse();
      System.out.println(strbuf);            //print Hello_Java World
      
      //setCharAt()
      strbuf.setCharAt(5,' ');
      System.out.println(strbuf);  
          //prit Hello Java World
      
      //charAt()
      System.out.print("Character at 6th position : ");
      System.out.println(strbuf.charAt(6));      //print J
  
      //substring()
      System.out.print("Substring from position 3 to 6 : ");
      System.out.println(strbuf.substring(3,7));    //print lo J
            
      //deleteCharAt()
      strbuf.deleteCharAt(3);
      System.out.println(strbuf);       
     //print Helo java World
      
      //capacity()
      System.out.print("Capacity of StringBuffer object : ");
      System.out.println(strbuf.capacity());      //print 21
      
      //delete() and length()
      strbuf.delete(6,strbuf.length());        
      System.out.println(strbuf);            //no anything
    }
    
catch(StringIndexOutOfBoundsException e){
      System.out.println(e.getMessage());
    }
  }
}

Download String Buffer Class Example

                         

Leave your comment:

Name:

Email:

URL:

Title:

Comments:


Enter Code:

Audio Version
Reload Image
 

Note: Emails will not be visible or used in any way, and are not required. Please keep comments relevant. Any content deemed inappropriate or offensive may be edited and/or deleted.

No HTML code is allowed. Line breaks will be converted automatically. URLs will be auto-linked. Please use BBCode to format your text.

Add This Tutorial To:
  Del.icio.us   Digg   Google   Spurl   Blink   Furl   Simpy   Y! MyWeb 

Current Comments

1 comments so far (
post your own) View All Comments Latest 10 Comments:

Mostly asked in interviews , string vs stringbufffer . gv more basic understanding.
while briefing just link the concept with the relative cincepts like collections in this case.

Posted by Manohar on Wednesday, 11.7.07 @ 10:02am | #36199

Latest Searches:
array to string
Photoshop Cut out imag
Facelet tutorial
Exercise - Pad Left
JSP:File Input Paramet
tables pdf itext
how will we set enviro
emailing file using js
MVC Architecture
form bean
howtoretrievedatabasei
weblogic 10.3
download tomcate 5.0
project for mobile pla
<html:options>
Ktoolbar TUTORIAL
drop box
rules
StringTokenizer specif
Date Examples java
basic about struts
Jtable Jbutton
sequence number genera
IBM MQ series
java is useful in netb
strating a thread in j
axis2 and ms exchange
Java example program t
fjhjf
median
Jtable
JSF datatree
Dependency list box in
jQuery To Slide Effect
java string
org.apache.poi.hssf.us
program for display th
passing values from js
PL/]SQL
selectonelistbox examp
Wireless
adding calculation
struts DAO connection
save query string
pseudocode
Tomcat deploy JSP
Load
sort object compareTo
how to include jsf tag
source code of puzzle
sum of n integers
read a row in oracle t
mysql insert statement
Binary search java
calculate
validation for char le
clear the console in j
ejp
get column count colu
data binding
permutation combinati
retreive numerical val
hibernate3.0 jar file
Photoshop Drawing 3D P
inventory
Error handling in stru
stored procedure
StringTokenizer select
LAMJ
JSP and MS access
tomcate 5.0 downloadab
videofile
url redirecting in ser
commands
flex
emailing file in java
standered jstl jar to
ะ�ะ�ะ�ะ?
Photoshop Textures and
arraylist of arraylist
Java String toLowerCase Example
Java String toCharArray Example
Java String substring Example
Java String indexOf Example
Java String startsWith Example
Java String hashCode Example
Java String matches Example
Java String length Example
Java String lastIndexOf Example
Java String isEmpty Example
Java String equalsIgnoreCase Example
Java String equals Example
Java String endsWith Example
Java String copyValueOf Example
Java String contentEquals Example
  EAI Articles
  Java Certification
Tell A Friend
Your Friend Name
Search Tutorials

 

 
 
Browse all Java Tutorials
Java JSP Struts Servlets Hibernate XML
Ajax JDBC EJB MySQL JavaScript JSF
Maven2 Tutorial JEE5 Tutorial Java Threading Tutorial Photoshop Tutorials Linux Technology
Technology Revolutions Eclipse Spring Tutorial Bioinformatics Tutorials Tools SQL
 

Home | JSP | EJB | JDBC | Java Servlets | WAP  | Free JSP Hosting  | Search Engine | News Archive | Jboss 3.0 tutorial | Free Linux CD's | Forum | Blogs

About Us | Advertising On RoseIndia.net  | Site Map

India News

Indian Software Development Company | iPhone Development Company in India | Java Training Delhi | Java Training at Noida |

Send your comments, Suggestions or Queries regarding this site at roseindia_net@yahoo.com.

Copyright © 2008. All rights reserved.