Register Now

Login

Lost Password

Enter your email to reset your password.

BY Author

Java – StringBuffer

The StringBuffer class is used when we want to create a string that can be modified.

Following are the constructors & methods used in a StringBuffer class.

Constructors

1) StringBuffer()

This constructor is used to create a StringBuffer object that has no string.
Initial capacity of any StringBuffer object is 16.

Example

 class SBConstructor1 
 {
     public static void main(String args[]) 
     {
         StringBuffer sb = new StringBuffer();
         System.out.println("Default Capacity :: "+sb.capacity());
     }
 }

Output

Default Capacity :: 16

2) StringBuffer(int size)

This constructor sets the size of the StringBuffer object.

Example

 class SBConstructor2 
 {
     public static void main(String args[]) 
     {
         StringBuffer sb = new StringBuffer(24);
         System.out.println("New Capacity :: "+sb.capacity());
     }
 }

Output

New Capacity :: 24

3) StringBuffer(String s)

This constructor initializes the StringBuffer object with specified string.

Example

 class SBConstructor3 
 {
     public static void main(String args[]) 
     {
         StringBuffer sb = new StringBuffer("Hello Java!");
         System.out.println(sb);
     }
 }

Output

Hello Java!

4) StringBuffer(CharSequrnce characters)

This constructor initializes the StringBuffer object with specified CharSequence.

Example

 class SBConstructor4 
 {
     public static void main(String args[]) 
     {
         CharSequence cs = "www.itsbeyondsimple.com";
         StringBuffer sb = new StringBuffer(cs);
         System.out.println(sb);
     }
 }

Output

www.itsbeyondsimple.com

Methods

1) Length()

This method returns the length of a string in StringBuffer. That is it represents the total number of characters present in a string.

Syntax

int length()

Example

 public class LengthDemo 
 {
     public static void main(String args[])
     {
         StringBuffer sb = new StringBuffer("GOOD MORNING"); 
         System.out.println("Length :: "+sb.length());
     }
 }

Output

Length :: 12

2) Capacity()

This method returns the current capacity of a StringBuffer object.

Syntax

int capacity()

Example

 public class CapacityDemo 
 {
     public static void main(String args[])
     {
         StringBuffer sb = new StringBuffer("GOOD MORNING"); 
         System.out.println("Capacity :: "+sb.capacity());
     }

 }

Output

Capacity :: 28

3) ensureCapacity()

This method is used to make sure that the capacity of the StringBuffer is equal or more than the specified capacity. If not, the capacity of StringBuffer is increased by [(current capacity * 2) +2] automatically.

Syntax

void ensureCapacity(int minimumCapacity)

Example

  public class EnsureCapacityDemo 
 {
	public static void main(String args[]) 
	{
		StringBuffer sb = new StringBuffer();
		System.out.println("Default Capacity :: "+sb.capacity());
		
		sb.ensureCapacity(10);   // 10 is less than current capacity, 
		                         //therefore no change in capacity
		System.out.println("New Capacity :: "+sb.capacity());
		
		sb.ensureCapacity(17);   // 17 is more than current capacity,
		                         // therefore capacity of StringBuffer increases
		                         // by formula, [(Current_Capacity*2)+2]
		System.out.println("New Capacity :: "+sb.capacity());
		
		sb.ensureCapacity(31);   // 31 is less than current capacity, 
                                         //therefore no change in capacity
		System.out.println("New Capacity :: "+sb.capacity());
		
		sb.ensureCapacity(35);   // 35 is more than current capacity,
                                         // therefore capacity of StringBuffer increases
                                         // by formula, [(Current_Capacity*2)+2]
             System.out.println("New Capacity :: "+sb.capacity());
	}
 }

Output

Default Capacity :: 16
New Capacity :: 16
New Capacity :: 34
New Capacity :: 34
New Capacity :: 70

4) setLength()

This method is used to set the length of a string in a StringBuffer object.

Syntax

void setLength(int length)

Example

 public class SetLengthDemo 
 {
     public static void main(String args[])
     {
         StringBuffer sb = new StringBuffer();
         System.out.println("Default length :: "+sb.length());
         sb.setLength(21);
         System.out.println("New length :: "+sb.length());
     }
 }

Output

Default length :: 0
New length :: 21

5) charAt()

This method returns a character present at the specified index of a string within a StringBuffer object.

Syntax

char charAt(int index)

Example

 public class CharAt 
 {
     public static void main(String args[]) 
     {
         String s = "Very Easy";
         System.out.println("Char at 0th index :: "+s.charAt(0));
         System.out.println("Char at 5th index :: "+s.charAt(5));
     }
 }

Output

Char at 0th index :: V
Char at 5th index :: E

Java Programming Language StringBuffer Class Method

6) setCharAt()

This method is used to set a specified character at the specified index.
The current character at the specified index is replaced by a new character.

Syntax

void setCharAt(int index, char ch)

Example

 public class SetCharAtDemo 
 {
     public static void main(String args[])
     {
         StringBuffer sb = new StringBuffer("GOLD");
         System.out.println(sb);
         sb.setCharAt(3, 'F');
         System.out.println(sb);
     }
 }

Output

GOLD
GOLF

7) append()

This method is used to append the specified substring or integer value or character value, etc. with a string present in invoking StringBuffer object.

Syntax

StringBuffer append(String s)

StringBuffer append(int val)

StringBuffer append(char ch)

StringBuffer append(Object ob)

Example

 public class AppendDemo 
 {
     public static void main(String args[])
     {
         StringBuffer sb = new StringBuffer("HE IS ");
         sb.append(102); // appending int
         sb.append(" YEAR OLD");  // appending string
         sb.append('!'); // appending character
         System.out.println(sb);	
     }
 }

Output

HE IS 102 YEAR OLD!

8) insert()

This method is used to insert a specified substring or integer value or character value, etc. at the specified index of an invoking StringBuffer object.

Syntax

StringBuffer insert(int index, String s)

StringBuffer insert(int index, char ch)

StringBuffer insert(int index, int val)

StringBuffer insert(int index, Object ob)

Example

 public class InsertDemo 
 {
     public static void main(String args[])
     {
         StringBuffer sb = new StringBuffer("I AM");
         sb.insert(4, " HANDSOME");
         System.out.println(sb);
     }
 }

Output

I AM HANDSOME

9) reverse()

This method is used to reverse the characters of a string.

Syntax

StringBuffer reverse()

Example

 public class ReverseDemo 
 {
     public static void main(String args[])
     {
         StringBuffer sb = new StringBuffer("I LOVE YOU");
         sb.reverse();
         System.out.println(sb);
     }
 }

Output

UOY EVOL I

10) deleteCharAt()

This method is used to delete a character present at the specified index of a string in invoking StringBuffer object.

Syntax

StringBuffer deleteCharAt(int index)

Example

 public class DeleteCharAtDemo 
 {
     public static void main(String args[]) 
     {
         StringBuffer sb = new StringBuffer("I Love Java");
         System.out.println("Before Deleting :: "+sb);
         sb.deleteCharAt(2);
         System.out.println("After Deleting :: "+sb);
     }
 }

Output

Before Deleting :: I Love Java
After Deleting :: I ove Java

11) delete()

This method is used to delete a group of characters starting from index start, up to index end-1.

Syntax

StringBuffer delete(int start, int end)

Example

 public class DeleteDemo 
 {
     public static void main(String args[]) 
     {
         StringBuffer sb = new StringBuffer("I LIKE JAVA");
         sb.deleteCharAt(7);
         System.out.println(sb);
         sb.delete(6, 10);
         System.out.println(sb);
     }
 }

Output

I LIKE AVA
I LIKE

12) replace()

This method is used to replace a group of characters with the specified substring.
Replacement occurs from startingIndex up to endIndex-1.

Syntax

StringBuffer replace(int startingIndex, int endIndex, String s)

Example

 public class ReplaceDemo 
 {
     public static void main(String args[]) 
     {
         StringBuffer sb = new StringBuffer("I HATE YOU");
         sb.replace(2, 6, "LOVE");
         System.out.println(sb);
     }
 }

Output

I LOVE YOU

13) substring()

This method is used to obtain a part of the string starting from an index start up to index end-1.

Syntax

String substring(int start, int end)

Example

 public class SubstringDemo 
 {
     public static void main(String args[])
     {
         StringBuffer sb = new StringBuffer("Its Beyond Simple");

         String s = sb.substring(4, 10);
         System.out.println(s);
     }
 }

Output

Beyond

Java – StringBuilder

Java StringBuilder is very similar to the StringBuffer except that it is non synchronized.

 

Leave a reply