StringBuilder Class in Java

In this tutorial, we will learn StringBuilder Class in Java and its application and methods used.

String Builder Class in Java is used to create mutable String. As we know in Java Strings are immutable and cannot be altered. But with the help of StringBuilder Class, we can convert the mutability of a String, making it very easy to modify and manipulate.

The StringBuilder class has three constructors

(1) StringBuilder ():- This constructor creates an empty string Builder with a default initial capacity as 16

(2) StringBuilder (String str):- This constructor creates a String Builder with the specified String str

(3) StringBuilder (int len):- This constructor creates an empty string Builder with a length of the line.

 

Methods Used in String Builder class in Java and Its application

 

(i)    StringBuilder append method:

This method concatenates the given arguments with the existing String

CODE :

import java.io.*;
class Code {
    public static void main(String[] args)throws IOException{
        BufferedReader in=new BufferedReader(new InputStreamReader(System.in));

        String str ="CodeSpeedy ";
        StringBuilder str1=new StringBuilder(str);
        System.out.println("Before Concatenation -> "+str1);
        str1.append("is awesome.");
        System.out.println("After Concatenation -> "+str1);
        
    }
}

OUTPUT:

Before Concatenation -> CodeSpeedy
After Concatenation -> CodeSpeedy is awesome.


Concatenation is done

 

(ii)   StringBuilder insert method:

This method inserts a given String of the argument passed into this String at a given position.

CODE:

import java.io.*;
class Code {
    public static void main(String[] args)throws IOException{
        BufferedReader in=new BufferedReader(new InputStreamReader(System.in));

        String str ="Subhojeet ";
        StringBuilder str1=new StringBuilder(str);
        System.out.println("Before inserting -> "+str1);
        str1.insert(3,"Ghosh");
        System.out.println("After inserting -> "+str1);
        
    }
}

OUTPUT:

Before inserting -> Subhojeet
After inserting -> SubGhoshhojeet

 

The String  “Subhojeet” is given and String “Ghosh” has to be inserted at position 3  (more precisely from position 3). Since String also has 0 based indexing so after “Sub” the String “Ghosh” gets inserted and the output is shown clearly states the same.

 

(iii)  StringBuilder replace  Method:

These methods replace the given range of String from this String with the String in the argument passed.

CODE:

import java.io.*;
class Code {
    public static void main(String[] args)throws IOException{
        BufferedReader in=new BufferedReader(new InputStreamReader(System.in));

        String str ="Subhojeet ";
        StringBuilder str1=new StringBuilder(str);
        System.out.println("Before replacing -> "+str1);
        str1.replace(1,4,"Ghosh");
        System.out.println("After replacing -> "+str1);
        
    }
}

OUTPUT:

Before replacing -> Subhojeet
After replacing -> SGhoshojeet

In this output the String argument passed “Ghosh” gets replaced with the String range of 1-4 and the result comes to “SGhoshojeet”

 

(iv)  StringBuilder Delete method:

This method delete a part of this String between the range specified in the argument.

CODE :

import java.io.*;
class Code {
    public static void main(String[] args)throws IOException{
        BufferedReader in=new BufferedReader(new InputStreamReader(System.in));

        String str ="Subhojeet ";
        StringBuilder str1=new StringBuilder(str);
        System.out.println("Before deleting -> "+str1);
        str1.delete(2,5);
        System.out.println("After deleting -> "+str1);
        
    }
}

OUTPUT:

Before deleting -> Subhojeet
After deleting -> Sujeet

As you can in the output that a part of the String got deleted from this String leaving behind the rest of it untouched.

 

(v)  StringBuilder reverse method:

The name is enough for us to determine what this method does. It reverses this String from its original one.

CODE :

import java.io.*;
class Code {
    public static void main(String[] args)throws IOException{
        BufferedReader in=new BufferedReader(new InputStreamReader(System.in));

        String str ="Subhojeet ";
        StringBuilder str1=new StringBuilder(str);
        System.out.println("Before reversing -> "+str1);
        str1.reverse();
        System.out.println("After reversing -> "+str1);
        
    }
}

OUTPUT:

Before reversing -> Subhojeet
After reversing ->  teejohbuS

From the output, it is very much clear that the String got reversed in no time with the help of the reverse method of the StringBuilder class.

 

(vi)  StringBuilder Substring method:

This method is a function overloading method. In the first, the function parameter has only the beginning index and in the second overloading method, it has a beginning index and end index.

CODE :

import java.io.*;
class Code {
    public static void main(String[] args)throws IOException{
        BufferedReader in=new BufferedReader(new InputStreamReader(System.in));

        String str ="Subhojeet ";
        StringBuilder str1=new StringBuilder(str);
        System.out.println("First Substring method(beginIndex) -> "+str1.substring(2));
        System.out.println("Second Substring method(beginIndex,endIndex)-> "+str1.substring(2,5));
        
    }
}

OUTPUT:

First Substring method(beginIndex) -> bhojeet
Second Substring method(beginIndex,endIndex)-> bho

This is how the String Builder class utilizes the substring function.

Hope you liked my explanation of String Builder Class and its various inbuilt methods.

HAPPY CODING !!!!!

Leave a Reply

Your email address will not be published.