StringBuffer in Java is the pre class of string class. StringBuffer also allows us to handle String message but in mutable behavior. It means once we assign a string value using StringBuffer then after we can grow or shrink the length of the message by adding or removing something.

Methods of StringBuffer in Java

append() Method

This Method adds new string message at last of StringBuffer class message.

Syntax

Obj.append(“append message”);

Example of append() Method in Java

Input Value

public class buffers {
    public static void main(String args[]){  
        StringBuffer a=new StringBuffer("Basic ");  
        a.append("engineer"); 
        System.out.println(a);  
        }  
}

Let’s Run the code

Output Value

insert() Method

This method adds the new message at desired place (index) in StringBuffer class.

Syntax

insert(index,”new string”);

Example of insert() Method in Java

Input Value

public class buffers {
    public static void main(String args[]){  
        StringBuffer a=new StringBuffer("Basic ");  
        a.insert(2,"Engineer"); 
        System.out.println(a);  
        }  
}

Let’s Run the code

Output Value

replace() Method

It replaces the desired position character(substring) with row substring.

Syntax

obj.replace (start,end-1,”new substring”);

Example of replace() Method in Java

Input Value

public class buffers {
    public static void main(String args[]){  
        StringBuffer a=new StringBuffer("Basic");  
        a.replace(3,6,"Engineer");  
        System.out.println(a); 
        }  
}

Let’s Run the code

Output Value

delete() Method

This method delete the given position character (word) from StringBuffer

Syntax

object.delete(start,end-1);

Example of delete() Method in Java

Input Value

public class buffers {
    public static void main(String args[]){  
        StringBuffer a=new StringBuffer("Basicengineer");  
        a.delete(4,8);  
        System.out.println(a);
        }  
}

Let’s Run the code

Output Value

reserve() Method

It reserve the string buffer message

Syntax

obj.reserve();

Example of reserve() Method in Java

Input Value

public class buffers {
    public static void main(String args[]){  
        StringBuffer a=new StringBuffer("basicengineer");  
        a.reverse();  
        System.out.println(a);
        }  
}

Let’s Run the code

Output Value

capacity() method

  • The capacity() method of StringBuffer class returns the current capacity of the buffer. The default capacity of the buffer is 16. If the number of character increases from its current capacity, it increases the capacity by (oldcapacity*2)+2.
  • For example if your current capacity is 16, it will be (16*2)+4=36.

Example of capacity() Method in Java

Input Value

public class buffers {
    public static void main(String args[]){  
        StringBuffer a=new StringBuffer();  
        System.out.println(a.capacity());  
        a.append("SSoftware");  
        System.out.println(a.capacity()); 
        a.append("Hello student basic Engineer");  
        System.out.println(a.capacity());  
        }  
}

Let’s Run the code

Output Value

If you have any queries regarding this article or if I have missed something on this topic, please feel free to add in the comment down below for the audience. See you guys in another article.

To know more about JAVA Wikipedia please click here .

Stay Connected Stay Safe, Thank you


Basic Engineer

Hey Readers! We have more than fifteen years of experience in Software Development, IoT, Telecom, Banking, Finance and Embedded domain. Currently we are actively working on Data Science, ML and AI with multiple market leaders worldwide. Happy Reading. Cheers!

0 Comments

Leave a Reply

Avatar placeholder

Your email address will not be published. Required fields are marked *