• In some cases, there may arise a need for the programmer to modify the content of the string and this is when the StringBuffer comes into play.
  • StringBuffer is similar to the String class except that String class is immutable, whereas the StringBuffer class is mutable (can be changed).
  • Later the StringBuffer objects can be converted back to String objects, so that the methods of the String class can be applied on them if necessary.

Creating StringBuffer objects:

StringBuffer can be defined simply by the use of new operator and by passing the string value inside the StringBuffer constructor.

Syntax:

StringBuffer <object> = new StringBuffer(String_value)

Example:
StringBuffer sb = new StringBuffer(“Hello”);
The other way is to create an empty object of StringBuffer and then adding the value using append or insert method.

Example:
StringBuffer sb1 = new StringBuffer();
sb1.append(“hi”);
sb1.insert(0, “hello”);  //inserting hello from the 0th position in sb1.
We can also mention the size of the StringBuffer while creating the StringBuffer object.

Example:
StringBuffer sb1 = new StringBuffer(100);
This indicates that we can add 100 characters in sb1 which can also be extended later.

Inserting and appending a String in StringBuffer:

Before inserting/appending a string, an empty StringBuffer object is created.

Syntax for insert():

StringBuffer insert(int p, x); // inserts x in the position p

Syntax for append():

StringBuffer append (x1);  // appends x1 to the StringBuffer

Example:
StringBuffer name = new StringBuffer();
names.insert(0,“Anjali”);
names.append(“Bissa”);

Deleting characters from the StringBuffer:

Deletion and insertion are similar.

Syntax:

StringBuffer delete(int p1, int p2)

This would delete the characters starting from the position p1 to p2-1th position.

Example:
StringBuffer name = new StringBuffer();
names.insert(0,“Anjali”);
names.append(“Bissa”);
names.delete(6,11); // deletes Bissa from name, name would contain Anjali

Reversing a string using StringBuffer:

Reversing a string is as simple as using reverse() method with the StringBuffer object.

Syntax:

StringBuffer reverse()

Example:
StringBuffer name= new StringBuffer(“madam”);
StringBuffer Reversed=name.reverse();  // Reversed would also have “madam”
The method toString() can be used to convert the StringBuffer object again to String object.

Finding the Length of the String in StringBuffer:

Finding the length of the StringBuffer is simple.

Syntax:

int length()

Example:

StringBuffer name= new StringBuffer(“Hello”);
int l = name.length(); // l would contain 5

Replacing characters of the string in StringBuffer:

Sequence of Characters can be replaced with a new set of characters using the replace() method.

Syntax:

StringBuffer replace(int i, int j, String s)

Replaces the characters starting from ith position till the j-1th position with the string s.

Example:
StringBuffer sb = new StringBuffer(“preorder”);
sb.replace(0,3,”post”);

Finding Substring using StringBuffer:

Syntax:

Stringbuffer substring(int i);

Retrieves the characters starting from the position i from the StringBuffer object.

Example:
StringBuffer sb = new StringBuffer(“BlackBoard”);
String s = sb.substring(5); // contains the substring Board

Syntax:

StringBuffer substring(int i, int j)

Retrieves the characters starting from the position i till the j-1th position.

Example:
StringBuffer sb = new StringBuffer(“BlackBoard”);
String s  = sb.substring(0,5); //contains the substring Black

Finding the index of a character in StringBuffer:

Position of a character(s) can be found using indexOf() method. Returns the first occurrence of the substring from left to right.

Syntax:

int indexOf(String str)

Example:

StringBuffer sb = new StringBuffer(“book”);
int i = sb.indexOf(“ok”);  // i contains 2
Similarly, last occurrence of the substring can also be found using lastIndexOf() method.

Syntax:

int lastIndexOf(String st)

Returns the last occurrence of the substring in the StringBuffer object. In other words, it finds the first occurrence of the substring from right to left.

Example:

StringBuffer sb = new StringBuffer(“This is an amazing book”);
int index = sb.lastIndexOf(“is”); //index would contain 5

StringBuffer Methods:

StringBuffer Methods StringBuffer Examples
StringBuffer append(x)
Where x can be of any datatype. It will be added to the StringBuffer object
StringBuffer sb1 = new StringBuffer(“fellow”);
sb1.append(“ship”);
//sb1 will contain fellowship
StringBuffer insert(int position, x)
Inserts the data x in the StringBuffer object starting from the given position.
StringBuffer sb1 = new StringBuffer(“Black”);
sb1.insert(6, “Board”)
//sb1 will contain Black Board
StringBuffer delete(int start, int end)
Deletes the data x in the StringBuffer object starting from the given position.
StringBuffer sb1 = new StringBuffer(“Black Board”);
sb1.delete(6, 10);
//sb1 will contain Black
StringBuffer reverse()
Reverses the character sequence present in the string.
StringBuffer sb1 = new StringBuffer(“fellow”);
sb1.reverse();
//sb1 will contain wollef
StringBuffer replace(int start, int end, String st)
Replaces the characters (in the StringBuffer object) from the start position to the end-1 position by the characters given in str.
StringBuffer sb1 = new StringBuffer(“fellow”);
sb1.replace(0,2,”ye”)
//sb1 will contain yellow
StringBuffer substring(int i)
Retrieves the string from the StringBuffer object starting from the ith position till the end.
StringBuffer substring(int i, int j)
Retrieves the string from the StringBuffer object starting from the ith position till the j-1th position.
StringBuffer sb1 = new StringBuffer(“fellow”);
sb1.substring(3);
//sb1 will contain low
StringBuffer sb1 = new StringBuffer(“fellow”);
sb1.substring(3,6);
//sb1 will also contain low
int length()
Returns the length of the StringBuffer object.
StringBuffer sb1 = new StringBuffer(“fellow”);
Int length = sb1.length();
//length will have 6
int indexOf(String str)
Returns the First Occurrence of the substring str in the StringBuffer object (from left to right)
StringBuffer sb1 = new StringBuffer(“fellow”);
Int low = sb1.indexOf(“low”);
//sb1 will have 3
int LastIndexOf(String str)
Returns the Last Occurrence of the substring str in the StringBuffer object (from left to right)
I.e. the first occurrence from the right.
StringBuffer sb1 = new StringBuffer(“He is part of this”);
Int low = sb1.LastIndexOf(“is”);
//sb1 will have 17

Some methods like insert(), delete(), reverse() etc are helpful which are not present in the String Class. The choice of using StringBuffer class is purely based on the requirement.