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.
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); } }
So, in the above example, we have created a StringBuilder object and initialised Hello to it.
StringBuilder x = new StringBuilder("Hello");
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.
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.
Now, let us see the second way using which we can create a StringBuilder object.
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); } }
In the above example, we have created an empty StringBuilder object,
StringBuilder x = new StringBuilder();
Now, in the next line, we have used the append() method of StringBuilder to insert Hello to the StringBuilder object,
x.append("Hello");
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");
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.
Now, if we see the output, HelloWorld is printed on the screen.
Say, we have a String HelloWorld and we want to insert a new String called Beautiful and make the new String HelloBeautifulWorld
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); } }
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.
Say, we have a String HelloWorld and we want to replace the subString Wor with Beautiful and make the new String HelloBeautifulld.
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); } }
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.
Say, we have a String HelloWorld and we want to delete the subString World and keep the new String Hello.
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); } }
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.
Say, we have a String HelloWorld and we want to reverse the String HelloWorld and make it dlroWolleH.
public class MyApplication { public static void main(String[] args) { StringBuilder x = new StringBuilder("HelloWorld"); x.reverse(); String s = x.toString(); System.out.println(s); } }
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.