StringBuffer and its methods with example

StringBuffer


  • StringBuffer is a peer class of String that provides much of the functionality of strings. 
  • String represents fixed-length, immutable character sequences.
  • StringBuffer represents growable and writable character sequences.
  • StringBuffer may have characters and substrings inserted in the middle or appended to the end. 
  • It will automatically grow to make room for such additions and often has more characters preallocated than are actually needed, to allow room for growth.

StringBuffer Constructors

  • StringBuffer( ): It reserves room for 16 characters without reallocation.
  • Example:

StringBuffer s=new StringBuffer();

  • StringBuffer(int size)It accepts an integer argument that explicitly sets the size of the buffer.
  • Example:

StringBuffer s=new StringBuffer(20);

  • StringBuffer(String str): It accepts a String argument that sets the initial contents of the StringBuffer object and reserves room for 16 more characters without reallocation.
  • Example:

StringBuffer s=new StringBuffer("Sarthak");

Methods

  • length( ) and capacity( ): The length of a StringBuffer can be found by the length( ) method, while the total allocated capacity can be found by the capacity( ) method.
  • Example:

int p = s.length();
int q = s.capacity();
System.out.println("Length of string"+ p);
System.out.println("Capacity of string" + q);

  • append( ): It is used to add text at the end of the existence text. Here are a few of its forms:
  • Example:

StringBuffer s = new StringBuffer("Sarthak");
s.append(" Patel");

  • insert( ): It is used to insert text at the specified index position. These are a few of its forms.
  • StringBuffer insert(int index, String str)
  • Example:

StringBuffer s = new StringBuffer("Sarthak");
s.insert(5, "for");

  • StringBuffer insert(int index, char ch)
  • Example:

StringBuffer s = new StringBuffer("Sarthak");
s.insert(0, 5);


  • reverse( ): It can reverse the characters within a StringBuffer object using reverse( ).This method returns the reversed object on which it was called. 
  • Example:

StringBuffer s = new StringBuffer("Sarthak");
s.reverse();


  • delete( ) and deleteCharAt( ): It can delete characters within a StringBuffer by using the methods delete( ) and deleteCharAt( ). The delete( ) method deletes a sequence of characters from the invoking object.The   deleteCharAt( ) method deletes the character at the index specified by loc.
  • StringBuffer delete(int startIndex, int endIndex)
  • Example:

StringBuffer s = new StringBuffer("Sarthak Patel");
s.delete(0, 5);

  • StringBuffer deleteCharAt(int loc)
  • Example:

StringBuffer s = new StringBuffer("Sarthak Patel");
s.deleteCharAt(7);


  • replace( ): It can replace one set of characters with another set inside a StringBuffer object by calling replace( ).
  • StringBuffer replace(int startIndex, int endIndex, String str)
  • Example:

StringBuffer s = new StringBuffer("Sarthak Patel");
s.replace(5, 8, "are");

Thanks a lot for query or your valuable suggestions related to the topic.

Previous Post Next Post

Contact Form