Tuesday 3 April 2012

Class - StringBuffer


StringBuffer

StringBuffer is a peer class of String that provides much of the functionality of strings.

StringBuffer represents growable and writeable character sequences. StringBuffer may have characters and substrings inserted in the middle or appended to the end.

StringBuffer will automatically grow to make room for such additions and often has more characters preallocated than are actually needed, to allow room for growth.

Constructors

StringBuffer defines these three constructors:

StringBuffer( )
StringBuffer(int size)
StringBuffer(String str)

The default constructor (the one with no parameters) reserves room for 16 characters without reallocation.

The second version accepts an integer argument thatexplicitly sets the size of the buffer.

 The third version accepts a String argument that sets the initial contents of the StringBuffer object and reserves room for 16 more characters without reallocation. StringBuffer allocates room for 16 additional characters when no specific buffer length is requested, because reallocation is a costly process in terms of time. Also, frequent reallocations can fragment memory. By allocating room for a few extra characters, StringBuffer reduces the number of reallocations that take place.

Methods

length( ) and capacity( )
                      int length( )

The current length of a StringBuffer can be found via the length( ) method


                      int capacity( )

The total allocated capacity can be found through the capacity( ) method. They have the
LIBRARY
charAt( ) and setCharAt( )

The value of a single character can be obtained from a StringBuffer via the charAt( )
method. You can set the value of a character within a StringBuffer using setCharAt( ).
Syntax:

                      char charAt(int where)
Here, where specifies the index of the character being obtained.

                      void setCharAt(int where, char ch)

setCharAt( ), where specifies the index of the character being set, and ch specifies the
new value of that character.

For both methods, where must be nonnegative and must not specify a location beyond the end of the buffer.

append()

The append() method concatenates the string representation of any other type of data to the end of the invoking StringBuffer object.
Syntax:
StringBuffer append(string str)
StringBuffer append(int num)
StringBuffer append(object obj)

insert()

The insert() method inserted one string into another. These are few of its forms:

It is overloaded to accept values of  all the simple types, plus Strings and Objects.

StringBuffer insert(int index,string str)
StringBuffer insert(int index,char ch)
StringBuffer insert(int index,object obj)

Here, index specifies the index at which point the string will be inserted into the inoking StringBuffer object.

reverse()

Synatax :

  StringBuffer reverse()
This method returns the reversed object on which it was called.

StringBuffer sb=new stringBuffer("India");
sb.reverse();
println(sb);
output:-aidnI

delete() and deletecharAt()

  StringBuffer delete(int StartIndex,int endIndex)
  StringBuffer deletecharAt(int loc)

The delete() method deletes a sequence of characters from the invoking object. Here,
StartIndex specifies the index of the first character to remove,and  endindex specifies an index one past the last character to remove.

The deletecharAt() method delets the character at the index specified by loc. It returns the resulting StringBuffer object.

Ex :

StringBuffer sb = new StringBuffer("ABCDEF")
sb.delete(2,4);
println(sb)
sb.deletecharAt(0);
println(sb)

replace( )

It replaces one set of characters with another set inside a StringBuffer object. It takes following form :

StringBuffer replace(int startIndex, int endIndex, String str)

The substring being replaced is specified by the indexes startIndex and endIndex. Thus, the substring at startIndex through endIndex–1 is replaced. The replacement string is passed in str. The resulting StringBuffer object is returned.

Posted By  : Ruchita  Panday

No comments:

Post a Comment