Logo 
Search:

Java Articles

Submit Article
Home » Articles » Java » FundamentalRSS Feeds

StringBuffer Class

Posted By: Harley Evans     Category: Java     Views: 4406

This article explains about StringBuffer class in java with examples.

StringBuffer class encapsulates a sequence of characters. A StringBuffer is same as String but it can be modified. StringBuffer object Content and Length can be changed through certain method calls. 

The main operations on a StringBuffer are the append and insert methods, which are overloaded so as to accept data of any type. Each effectively converts a given data to a string and then appends or inserts the characters of that string to the string buffer. The append method always adds these characters at the end of the buffer and the insert method adds the characters at a specified point.

Below are the constructors and methods available in StringBuffer class.

Constructors in StringBuffer Class

 Constructor

Description 

 StringBuffer() 

Constructs a string buffer with no characters in it and an initial capacity of 16 characters. 

 StringBuffer(int length) 

Constructs a string buffer with no characters in it and an initial capacity specified by the length argument. 

 StringBuffer(String str)

Constructs a string buffer so that it represents the same sequence of characters as the string argument, in other words, the initial contents of the string buffer is a copy of the argument string. 

 


Methods of StringBuffer Class

Method 

Description 

StringBuffer append(boolean b) 

Appends the string representation of the boolean argument to the string buffer.  

StringBuffer append(char c)  

Appends the string representation of the char argument to this string buffer. 

StringBuffer append(char[] str) 

Appends the string representation of the char array argument to this string buffer. 

StringBuffer append(char[] str, int offset, int len)  

Appends the string representation of a subarray of the char array argument to this string buffer. 

StringBuffer append(double d)  

Appends the string representation of the double argument to this string buffer. 

StringBuffer append(float f)  

Appends the string representation of the float argument to this string buffer. 

StringBuffer append(int i)  

Appends the string representation of the int argument to this string buffer. 

StringBuffer append(long l) 

Appends the string representation of the long argument to this string buffer. 

StringBuffer append(Object obj)  

Appends the string representation of the Object argument to this string buffer. 

StringBuffer append(String str)  

Appends the string to this string buffer. 

StringBuffer append(StringBuffer sb)  

Appends the specified StringBuffer to this StringBuffer. 

int capacity()  

Returns the current capacity of the String buffer. 

char charAt(int index)  

The specified character of the sequence currently represented by the string buffer, as indicated by the index argument, is returned. 

StringBuffer delete(int start, int end)  

Removes the characters in a substring of this StringBuffer. 

StringBuffer deleteCharAt(int index) 

Removes the character at the specified position in this StringBuffer (shortening the StringBuffer by one character). 

voidensureCapacity(int minimumCapacity)  

Ensures that the capacity of the buffer is at least equal to the specified minimum. 

voidgetChars(int srcBegin, int srcEnd, char[] dst, int dstBegin)  

Characters are copied from this string buffer into the destination character array dst. 

int indexOf(String str)  

Returns the index within this string of the first occurrence of the specified substring. 

 int indexOf(String str, int fromIndex) 

Returns the index within this string of the first occurrence of the specified substring, starting at the specified index. 

StringBuffer insert(int offset, boolean b)  

Inserts the string representation of the boolean argument into this string buffer. 

StringBuffer insert(int offset, char c)  

Inserts the string representation of the char argument into this string buffer. 

StringBuffer insert(int offset, char[] str)  

Inserts the string representation of the char array argument into this string buffer. 

StringBuffer insert(int index, char[] str, int offset, int len)  

Inserts the string representation of a subarray of the str array argument into this string buffer. 

StringBuffer insert(int offset, double d)  

Inserts the string representation of the double argument into this string buffer. 

StringBuffer insert(int offset, float f)  

Inserts the string representation of the float argument into this string buffer. 

StringBuffer insert(int offset, int i)  

Inserts the string representation of the second int argument into this string buffer. 

StringBuffer insert(int offset, long l)  

Inserts the string representation of the long argument into this string buffer. 

StringBuffer insert(int offset, Object obj)  

Inserts the string representation of the Object argument into this string buffer. 

StringBuffer insert(int offset, String str) 

Inserts the string into this string buffer. 

int lastIndexOf(String str)  

Returns the index within this string of the rightmost occurrence of the specified substring. 

int lastIndexOf(String str, int fromIndex)  

Returns the index within this string of the last occurrence of the specified substring. 

int length()  

Returns the length (character count) of this string buffer. 

StringBuffer replace(int start, int end, String str)  

Replaces the characters in a substring of this StringBuffer with characters in the specified String. 

StringBuffer reverse()  

The character sequence contained in this string buffer is replaced by the reverse of the sequence. 

voidsetCharAt(int index, char ch)  

The character at the specified index of this string buffer is set to ch. 

voidsetLength(int newLength)

Sets the length of this String buffer. 

CharSequence subSequence(int start, int end)  

Returns a new character sequence that is a subsequence of this sequence. 

String substring(int start)  

Returns a new String that contains a subsequence of characters currently contained in this StringBuffer.The substring begins at the specified index and extends to the end of theStringBuffer. 

String substring(int start, int end) 

Returns a new String that contains a subsequence of characters currently contained in this StringBuffer. 

String toString() 

Converts to a string representing the data in this string buffer. 



Example of StringBuffer Class

Example 1 : Append different type of data in StringBuffer class

class StringBufferDemo
{
  public static void main(String[] args) 
  {

    StringBuffer strb = new StringBuffer();

    strb.append("Hello ");

    char[] chArr = { 'W', 'o', 'r', 'l', 'd' };

    strb.append(chArr);

    strb.append(" ");

    strb.append(2010);

    strb.append(" ");

    strb.append(20.10);

    System.out.println(strb);

  }
}

Output 

Hello World 2010 20.10
  
Share: 

 
 
 

Didn't find what you were looking for? Find more on StringBuffer Class Or get search suggestion and latest updates.

Harley Evans
Harley Evans author of StringBuffer Class is from London, United Kingdom.
 
View All Articles

 
Please enter your Comment

  • Comment should be atleast 30 Characters.
  • Please put code inside [Code] your code [/Code].

 
No Comment Found, Be the First to post comment!