Learnerslesson
   JAVA   
  SPRING  
  SPRINGBOOT  
 HIBERNATE 
  HADOOP  
   HIVE   
   ALGORITHMS   
   PYTHON   
   GO   
   KOTLIN   
   C#   
   RUBY   
   C++   




Java - StringBuilder


Java StringBuilder class is used to create String objects that can be changed or modified.


It is same as the StringBuffer class with a minor difference. i.e. StringBuilder class is not Synchronised.


We will seeing two ways by which we can use the java StringBuilder class.


First way of creating a StringBuilder object


Example :



public class MyApplication {
    public static void main(String[] args) {

        StringBuilder x = new StringBuilder("Hello");
        x.append("World");

        String s = x.toString();
        System.out.println(s);
    }
}


Output :



  HelloWorld

So, in the above example, we have created a StringBuilder object and initialised Hello to it.


StringBuilder x = new StringBuilder("Hello");
Spring_Boot


In the next line, we are going to club World with Hello and make it HelloWorld.


To achieve that we would be using the append() method provided by StringBuilder.


x.append("World");

So, the append() method adds the String World with Hello and we get the String HelloWorld.

Spring_Boot

In the next line, we have created a String x and initialised it with the StringBuilder object after converting the StringBuilder object to String.


String s = x.toString();

And if we see the output, we can see that HelloWorld is printed on the screen.

Output :



  HelloWorld

Now, let us see the second way using which we can create a StringBuilder object.


Second way of creating a StringBuilder object


Example :



public class MyApplication {
    public static void main(String[] args) {

        StringBuilder x = new StringBuilder();
        x.append("Hello");
        x.append("World");

        String s = x.toString();
        System.out.println(s);
    }
}


Output :



  HelloWorld

In the above example, we have created an empty StringBuilder object,


StringBuilder x = new StringBuilder();
Spring_Boot


Now, in the next line, we have used the append() method of StringBuilder to insert Hello to the StringBuilder object,


x.append("Hello");
Spring_Boot


Then in the next line, we have used the append() method again to add the String World after Hello and make it HelloWorld.


x.append("World");
Spring_Boot


Then we are using the toString() method of StringBuilder to convert the StringBuilder object to a String object.


String s = x.toString();

And assign it to a String type variable x.

Spring_Boot

Now, if we see the output, HelloWorld is printed on the screen.

Output :



  HelloBeautifulWorld

Other ways of using StringBuilder class


Using the insert() method of StringBuilder class


Say, we have a String HelloWorld and we want to insert a new String called Beautiful and make the new String HelloBeautifulWorld


Example :



public class MyApplication {
    public static void main(String[] args) {

        StringBuilder x = new StringBuilder("HelloWorld");

        x.insert(5, "Beautiful");

        String s = x.toString();
        System.out.println(s);
    }
}


Output :



  HelloBeautifulWorld

So, in the above example, we have a String HelloWorld and we have initialised it to a StringBuilder object.


StringBuilder x = new StringBuilder("HelloWorld");

Now, we want to insert a new String called Beautiful inside Hello and World.


In other words, we have to insert the String Beautiful in 5th index position (i.e. Right after the String Hello).


x.insert(5, "Beautiful");

And if we see the output, HelloBeautifulWorld is printed on the screen.

Output :



  HelloBeautifulWorld

Using the replace() method of StringBuilder class


Say, we have a String HelloWorld and we want to replace the subString Wor with Beautiful and make the new String HelloBeautifulld.


Example :



public class MyApplication {
    public static void main(String[] args) {

        StringBuilder x = new StringBuilder("HelloWorld");

        x.replace(5, 8, "Beautiful");

        String s = x.toString();
        System.out.println(s);
    }
}


Output :



  HelloBeautifulld

So, in the above example, we have a String HelloWorld and we have initialised it to a StringBuilder object.


StringBuilder x = new StringBuilder("HelloWorld");

In the next line, we are going to replace the subString Wor with the String Beautiful.


x.replace(5, 8, "Beautiful");

And if we see the output, HelloBeautifulld is printed on the screen.

Output :



  HelloBeautifulld

Using the delete() method of StringBuilder class


Say, we have a String HelloWorld and we want to delete the subString World and keep the new String Hello.


Example :



public class MyApplication {
    public static void main(String[] args) {

        StringBuilder x = new StringBuilder("HelloWorld");

        x.delete(5,10);

        String s = x.toString();
        System.out.println(s);
    }
}


Output :



  Hello

So, in the above example, we have a String HelloWorld and we have initialised it to a StringBuilder object.


StringBuilder x = new StringBuilder("HelloWorld");

In the next line, we are going to delete the subString World from the String HelloWorld.


x.delete(5,10);

And if we see the output, Hello is printed on the screen.

Output :



  Hello

Using the reverse() method of StringBuilder class


Say, we have a String HelloWorld and we want to reverse the String HelloWorld and make it dlroWolleH.


Example :



public class MyApplication {
    public static void main(String[] args) {

        StringBuilder x = new StringBuilder("HelloWorld");

        x.reverse();

        String s = x.toString();
        System.out.println(s);
    }
}


Output :



  dlroWolleH

The above code is self explanatory.


We have used the reverse() method of the StringBuilder class to reverse the String HelloWorld and make it dlroWolleH.


x.reverse();

And we got the below reversed String as output.

Output :



  dlroWolleH