StringBuffer Class Methods

Commonly used methods of StringBuffer class are:-


length()

The current length of the StringBuffer can be found via the length() method. Length of the StringBuffer include the total number of characters in the StringBuffer Object.
Syntax:

public int length()

//finding the length of StringBuffer
class Example {
    public static void main(String[] args) {
        String str = "Java is Robust Language";

        //creating the object of StringBuffer
        StringBuffer br = new StringBuffer(str);

        //finding the length
        int l = br.length();
        System.out.println("Length =" + l);
    }

}
Length =23

capacity()

This method is used to find the total allocated capacity of the StringBuffer object.
We know that , whenever a StringBuffer object is created the JRE creates a default array for it and the default capacity of array is defined to 16 .This capacity is growable in nature that means capacity of StringBuffer object increases as the size of the StringBuffer object increases.
Syntax:

public int capacity()

//finding the capacity of StringBuffer
class Example {

    public static void main(String[] args) {
        //going to create three object of StringBuffer 
        StringBuffer br1 = new StringBuffer();
        StringBuffer br2 = new StringBuffer("Hello");
        StringBuffer br3 = new StringBuffer(10);

        //printing the capacity of all StringBuffer
        System.out.println("Capacity of br1 : " + br1.capacity());
        System.out.println("Capacity of br2 : " + br2.capacity());
        System.out.println("Capacity of br3 : " + br3.capacity());
    }

}
Capacity of br1 : 16
Capacity of br2 : 21
Capacity of br3 : 10

append()

This method is used to concatinate the string representation of any other type of data to the end of invoking StringBuffer object.
Syntax:


  public StringBuffer append(Object ob);
  public StringBuffer append(String str);
  public StringBuffer append(StringBuffer br);
  public StringBuffer append(char char);
  public StringBuffer append(int  i);
  public StringBuffer append(long l);
  public StringBuffer append(float f);
  public StringBuffer append(double d)

class Example {

    public static void main(String[] args) {
        //going to create object of StringBuffer 
        StringBuffer br1 = new StringBuffer("Hello");
        StringBuffer newbr = br1.append("world");
        System.out.println(newbr);
    }

}
Helloworld

replace()

This method help us to replace a part of StringBuffer object with other string .
Syntax :

public StringBuffer replace(int start , int end-1 , string str);

It replaces the character starting from start till end-1 with str.

class Example {
    public static void main(String[] args) {
        StringBuffer br = new StringBuffer("Ram is a good boy");
        System.out.println(br);
        //now replacing Ram with Shyam
        br.replace(0, 3, "Shyam");
        //since , br is StringBuffer object  changes are done in br itself
        System.out.println(br);
    }
}
Ram is a good boy
Shyam is a good boy

delete() and deleteCharAt()

We can delete characters within a StringBuffer by using the methods delete() and deleteCharAt(). Basically these methods are used for deleting a part of the StringBuffer object according to the index passed in the parameter. Signature of these method are shown below:


public StringBuffer delete(int startIndex , int endIndex-1)
public StringBuffer deleteCharAt(int index)

class Example {

    public static void main(String[] args) {
        StringBuffer br = new StringBuffer("Ram is a good boy");
        System.out.println(br);
        //now deleting some characters

        br.deleteCharAt(0);

        System.out.println(br);

        br.delete(4, 8);

        //since, br is StringBuffer object changes are done in br itself
        System.out.println(br);
    }

}
Ram is a good boy
am is a good boy
am igood boy

insert()

insert() method is used for inserting one string into another string at a given index.
Syntax:


public StringBuffer insert(int index , String  str )
public StringBuffer insert(int index , char ch)

class Example {

    public static void main(String[] args) {
        StringBuffer br = new StringBuffer("Ram is a good boy");
        System.out.println(br);
       
        br.insert(7, " not ");

        //since, br is StringBuffer object changes are done in br itself
        System.out.println(br);
    }

}
Ram is a good boy
Ram is  not a good boy

reverse()

This method is used to reverse the characters within StringBuffer Object .
Syntax:


public StringBuffer reverse()

class Example {

    public static void main(String[] args) {
        StringBuffer br = new StringBuffer("Ram is a good boy");
        System.out.println(br);
       
        br.reverse();

        //since, br is StringBuffer object changes are done in br itself
        System.out.println(br);
    }

}
Ram is a good boy
yob doog a si maR

toString()

This method is used to convert the current invoking StringBuffer object into String form .
This is a method of java.lang.Object . StringBuffer class overrides this method .
Syntax:


public String toString()


class Example {

    public static void main(String[] args) {
        StringBuffer br = new StringBuffer("Ram is a good boy");
        System.out.println(br);

        String str = br.toString();

        System.out.println(str);
    }

}
Ram is a good boy
Ram is a good boy
Example) Program to take the string from the user and reverse each word of string and count length of each words in the string.
import java.util.Scanner;

class Example {

    public static void main(String[] args) {
        //creating Scanner object
        Scanner sc = new Scanner(System.in);

        System.out.println("Enter string here :");
        //taking the input using nextLine() method of Scanner
        String str = sc.nextLine();

        //spliting  words using split() method of string class
        String words[] = str.split(" ");

        for (int i = 0; i < words.length; i++) {

            String word = words[i];
            StringBuffer br = new StringBuffer(words[i]);
            System.out.println(word + " : " + br.reverse() + " : " + br.length());
        }
    }

}
Enter string here :
java is one of the most popular programming lang
java : avaj : 4
is : si : 2
one : eno : 3
of : fo : 2
the : eht : 3
most : tsom : 4
popular : ralupop : 7
programming : gnimmargorp : 11
lang : gnal : 4