Dremendo Tag Line

StringBuffer in Java Programming

String in Java

In this lesson, we will understand what is StringBuffer in Java Programming along with some examples.

What is StringBuffer in Java

A StringBuffer is a class in java which is used to store a thread-safe, mutable sequence of characters. A StringBuffer is like a String, but can be modified.

video-poster

Syntax of Creating a StringBuffer Object

StringBuffer object_name=new StringBuffer("string value");

Example

StringBuffer sb=new StringBuffer("Dremendo");

Here we have declared a StringBuffer object sb and initialize it with the string value Dremendo.

Methods of StringBuffer Class

StringBuffer methods are useful functions used to perform a specific task on the content of the StringBuffer object. Below we have discussed all the important StringBuffer methods available in the StringBuffer class.

length() Method

The length() method returns the total number of characters (including space) present in the string buffer object.

Example

import java.util.Scanner;

public class StringBufferMethod
{
    public static void main(String args[])
    {
        String s;
        int l;
        Scanner sc=new Scanner(System.in);

        System.out.println("Enter a string");
        s=sc.nextLine();
        StringBuffer sb=new StringBuffer(s);

        l=sb.length();
        System.out.println("Length="+l);
    }
}

Output

Enter a string
Hello World
Length=11

charAt() Method

The charAt() method returns the character at the specified index. In a string buffer object, the first character starts from index 0.

Example

import java.util.Scanner;

public class StringBufferMethod
{
    public static void main(String args[])
    {
        String s;
        int l;
        Scanner sc=new Scanner(System.in);

        System.out.println("Enter a string");
        s=sc.nextLine();
        StringBuffer sb=new StringBuffer(s);

        System.out.println("Character at index 3="+sb.charAt(3));
    }
}

Output

Enter a string
Dremendo
Character at index 3=m

setCharAt() Method

The setCharAt() method set a specified character at the specified index in the string buffer object.

Example

import java.util.Scanner;

public class StringBufferMethod
{
    public static void main(String args[])
    {
        String s;
        Scanner sc=new Scanner(System.in);

        System.out.println("Enter a string");
        s=sc.nextLine();

        StringBuffer sb=new StringBuffer(s);
        sb.setCharAt(2,'m');
        System.out.println(sb);
    }
}

Output

Enter a string
Tiger
Timer

deleteCharAt() Method

The deleteCharAt() method removes a character at the specified index in the string buffer object.

Example

import java.util.Scanner;

public class StringBufferMethod
{
    public static void main(String args[])
    {
        String s;
        Scanner sc=new Scanner(System.in);

        System.out.println("Enter a string");
        s=sc.nextLine();

        StringBuffer sb=new StringBuffer(s);
        sb.deleteCharAt(2);
        System.out.println(sb);
    }
}

Output

Enter a string
Tiger
Tier

delete() Method

The delete() method removes character sequences from the specified start index till end index-1 in a string buffer object.

Example

import java.util.Scanner;

public class StringBufferMethod
{
    public static void main(String args[])
    {
        String s;
        Scanner sc=new Scanner(System.in);

        System.out.println("Enter a string");
        s=sc.nextLine();

        StringBuffer sb=new StringBuffer(s);
        sb.delete(1,5);
        System.out.println(sb);
    }
}

Output

Enter a string
Computer
Cter

indexOf() Method

The indexOf() method returns the index of the first occurrence of the specified character or string within the current string buffer object. The indexOf() method accepts two arguments. The first argument is the string or character and the second optional argument is the index from where we want to check the occurrence. If the index is not given, it will start checking the occurrence from index 0.

Example

import java.util.Scanner;

public class StringBufferMethod
{
    public static void main(String args[])
    {
        String s1,s2;
        Scanner sc=new Scanner(System.in);

        System.out.println("Enter 2 strings");
        s1=sc.nextLine();
        s2=sc.nextLine();
        StringBuffer sb=new StringBuffer(s1);

        System.out.println("The 2nd string first occur at index " + sb.indexOf(s2) + " in the string buffer object.");
        System.out.println("After index 5, the 2nd string first occur at index " + sb.indexOf(s2,5) + " in the string buffer object.");
    }
}

Output

Enter 2 strings
I am a boy and I love java programming.
I
The 2nd string first occur at index 0 in the string buffer object.
After index 5, the 2nd string first occur at index 15 in the string buffer object.

lastIndexOf() Method

The lastIndexOf() method returns the index of the last occurrence of the specified character or string within the current string buffer object.

Example

import java.util.Scanner;

public class StringBufferMethod
{
    public static void main(String args[])
    {
        String s1,s2;
        Scanner sc=new Scanner(System.in);

        System.out.println("Enter 2 strings");
        s1=sc.nextLine();
        s2=sc.nextLine();
        StringBuffer sb=new StringBuffer(s1);

        System.out.println("The 2nd string last occur at index " + sb.lastIndexOf(s2) + " in the string buffer object.");
    }
}

Output

Enter 2 strings
I am a boy and I love playing football.
I
The 2nd string last occur at index 15 in the string buffer object.

toString() Method

The toString() method returns a string representing the data of the string buffer object.

Example

import java.util.Scanner;

public class StringBufferMethod
{
    public static void main(String args[])
    {
        String s1,s2;
        Scanner sc=new Scanner(System.in);

        System.out.println("Enter a string");
        s1=sc.nextLine();
        StringBuffer sb=new StringBuffer(s1);
        s2=sb.toString();

        System.out.println(s2);
    }
}

Output

Enter a string
Java Programming
Java Programming

append() Method

The append() method appends the specified string to the end of the current string buffer object.

Example

import java.util.Scanner;

public class StringBufferMethod
{
    public static void main(String args[])
    {
        String s1,s2;
        Scanner sc=new Scanner(System.in);

        System.out.println("Enter 2 strings");
        s1=sc.nextLine();
        s2=sc.nextLine();

        StringBuffer sb=new StringBuffer(s1);
        sb.append(s2);
        System.out.println(sb);
    }
}

Output

Enter 2 strings
good
bye
goodbye

replace() Method

The replace() method replaces a substring with the specified string in the string buffer object.

Example

import java.util.Scanner;

public class StringBufferMethod
{
    public static void main(String args[])
    {
        String s1,s2;
        Scanner sc=new Scanner(System.in);

        System.out.println("Enter 2 strings");
        s1=sc.nextLine();
        s2=sc.nextLine();

        StringBuffer sb=new StringBuffer(s1);
        sb.replace(7,13,s2);
        System.out.println(sb);
    }
}

Output

Enter 2 strings
I Love Orange
Apple
I Love Apple

insert() Method

The insert() method insert a specified string in the string buffer object at the specified index.

Example

import java.util.Scanner;

public class StringBufferMethod
{
    public static void main(String args[])
    {
        String s1,s2;
        Scanner sc=new Scanner(System.in);

        System.out.println("Enter 2 strings");
        s1=sc.nextLine();
        s2=sc.nextLine();

        StringBuffer sb=new StringBuffer(s1);
        sb.insert(0,s2);
        System.out.println(sb);
    }
}

Output

Enter 2 strings
how are you
Hello
Hello how are you

reverse() Method

The reverse() method replaces the character sequence of the string buffer object by the reverse of the sequence.

Example

import java.util.Scanner;

public class StringBufferMethod
{
    public static void main(String args[])
    {
        String s;
        Scanner sc=new Scanner(System.in);

        System.out.println("Enter a string");
        s=sc.nextLine();

        StringBuffer sb=new StringBuffer(s);
        sb.reverse();
        System.out.println(sb);
    }
}

Output

Enter a string
Apple
elppA

substring() Method

The substring() method returns a new string which is a substring currently contained in the string buffer object.

Example

import java.util.Scanner;

public class StringBufferMethod
{
    public static void main(String args[])
    {
        String s;
        Scanner sc=new Scanner(System.in);

        System.out.println("Enter a string");
        s=sc.nextLine();

        StringBuffer sb=new StringBuffer(s);

        System.out.println("Print the 1st 3 characters of the current string");
        System.out.println(sb.substring(0,3));
        System.out.println("Print the 1st 3 characters starting from index 2 and stopping at index 5");
        System.out.println("The stopping index is not included in the substring");
        System.out.println(sb.substring(2,5));
        System.out.println("Print all the characters starting from index 3 till end");
        System.out.println(sb.substring(3));
    }
}

Output

Enter a string
Dremendo
Print the 1st 3 characters of the current string
Dre
Print the 1st 3 characters starting from index 2 and stopping at index 5
The stopping index is not included in the substring
eme
Print all the characters starting from index 3 till end
mendo

How to compare the data of two string buffer objects?

To compare the data of two string buffer object, first, we have to convert their data into the string and then apply the compareTo() or compareToIgnoreCase() function of the String class on it. Let's see the example given below.

Example

import java.util.Scanner;

public class StringBufferMethod
{
    public static void main(String args[])
    {
        Scanner sc=new Scanner(System.in);

        System.out.println("Enter 2 strings");
        StringBuffer sb1=new StringBuffer(sc.nextLine());
        StringBuffer sb2=new StringBuffer(sc.nextLine());

        if(sb1.toString().compareTo(sb2.toString())==0)
        {
            System.out.println("Both the strings are same");
        }
        else
        {
            System.out.println("Both the strings are not same");
        }
    }
}

Output

Enter 2 strings
Computer
Computer
Both the strings are same

How to overwrite a string buffer object with a new string?

To overwrite a string buffer object with a new string, we have to replace the old string with the new one. Let's see the example given below.

Example

import java.util.Scanner;

public class StringBufferMethod
{
    public static void main(String args[])
    {
        Scanner sc=new Scanner(System.in);
        String s;

        System.out.println("Enter 2 strings");
        StringBuffer sb=new StringBuffer(sc.nextLine());
        s=sc.nextLine();

        System.out.println("Current string in string buffer object = " + sb);
        sb.replace(0,sb.length(),s);
        System.out.println("New string in string buffer object = " + sb);
    }
}

Output

Enter 2 strings
Apple
Ball
Current string in string buffer object = Apple
New string in string buffer object = Ball