Categories: Java

Java StringBuffer class

StringBuffer is a String peer class that provides a lot of the same functionality as strings. StringBuffer represents growable and writable character sequences, whereas a string represents fixed-length, immutable character sequences. Characters and substrings can be inserted in the middle or appended to the end of a StringBuffer.

Constructors of StringBuffer class :

1. StringBuffer(): It saves 16 characters by not reallocating space.

StringBuffer s = new StringBuffer();

2. StringBuffer( int size): It accepts an integer argument that specifies the buffer’s size.

StringBuffer s = new StringBuffer(20);

3. StringBuffer(String str): It takes a string argument that sets the initial contents of the StringBuffer object and frees up space for 16 more characters without reallocating space.

StringBuffer s = new StringBuffer("studentscoding");
Important methods of StringBuffer class:
Modifier and TypeMethodDescription
public synchronized StringBufferappend(String s)It is used to append the specified string with this string. Like append(char), append(boolean), append(int), append(float), append(double), etc., the append() function has many overloads.
public synchronized StringBufferinsert(int offset, String s)It is used to insert the specified string with this string at the specified position. There are several other overloaded versions of the insert() method, including insert(int, char), insert(int, boolean), insert(int, int), insert(int, float), insert(int, double), etc.
public synchronized StringBufferreplace(int startIndex, int endIndex, String str)It replaces the string from specified startIndex and endIndex.
public synchronized StringBufferdelete(int startIndex, int endIndex)It deletes the string from specified startIndex and endIndex.
public synchronized StringBufferreverse()it reverses the string.
public intcapacity()It returns the current capacity.
public voidensureCapacity(int minimumCapacity)It ensures the capacity is at least equal to the given minimum.
public charcharAt(int index)It returns the character at the specified position.
public intlength()It returns the length of the string i.e. total number of characters.
public Stringsubstring(int beginIndex)It returns the substring from the specified beginIndex.
public Stringsubstring(int beginIndex, int endIndex)It returns the substring from the specified beginIndex and endIndex.
Example: length(),capacity(),append(), reverse(), replace(), ensurecapacity()

import java.io.*;

// Main class
class Demo {

 // main driver method
 public static void main(String[] args)
 {

  // Creating adn storing string by creating object of
  // StringBuffer
  StringBuffer s = new StringBuffer("Coderzpy");

  // Getting the length of the string
  int p = s.length();

  // Getting the capacity of the string
  int q = s.capacity();
   System.out.println("Length of string "+s+"="
      + p);
  System.out.println(
   "Capacity of string "+s+"=" + q);
  s.append(".com");

  
  System.out.println("Length of string "+s+"="
      + s.length());
  System.out.println(
   "Capacity of string "+s+"=" + s.capacity());
   System.out.println("reverse = "+s.reverse());
   
   
        s.deleteCharAt(7);
        // Returns string after deletion
        System.out.println("deleted "+s);
        
         s.replace(5, 8, "are");
 
       
        System.out.println("replaced with are"+s);

        s.ensureCapacity(40);
          System.out.println(
   "Capacity of string "+s+"=" + s.capacity());
        
 }
}
Output:
Length of string Coderzpy=8
Capacity of string Coderzpy=24
Length of string Coderzpy.com=12
Capacity of string Coderzpy.com=24
reverse = moc.ypzredoC
deleted moc.ypzedoC
replaced with aremoc.yaredoC
Capacity of string moc.yaredoC=50

Note: also read about the Some examples of string class methods

Follow Me

If you like my post, please follow me to read my latest post on programming and technology.

https://www.instagram.com/coderz.py/

https://www.facebook.com/coderz.py

Share
Published by
Rabecca Fatima

Recent Posts

Select a Random Element from a Stream

You are given a stream of elements that is too large to fit into memory.…

2 days ago

Estimate π Using Monte Carlo Method

The formula for the area of a circle is given by πr². Use the Monte…

3 weeks ago

Longest Substring with K Distinct Characters

Given an integer k and a string s, write a function to determine the length…

3 weeks ago

Staircase Climbing Ways

There is a staircase with N steps, and you can ascend either 1 step or…

4 weeks ago

Autocomplete System Implementation

Build an autocomplete system that, given a query string s and a set of possible…

4 weeks ago

Job Scheduler Implementation

Design a job scheduler that accepts a function f and an integer n. The scheduler…

4 weeks ago