Once a String type object is created, it cannot be changed or modified.
And this is where StringBuffer comes handy.
Java StringBuffer class is used to create String objects that can be changed or modified.
And the StringBuffer class is thread safe.
We will seeing two ways by which we can use the java StringBuffer class.
public class MyApplication { public static void main(String[] args) { StringBuffer x = new StringBuffer("Hello"); x.append("World"); String s = x.toString(); System.out.println(s); } }
So, in the above example, we have created a StringBuffer object and initialised Hello to it.
StringBuffer x = new StringBuffer("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 StringBuffer.
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 StringBuffer object after converting the StringBuffer 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 StringBuffer object.
public class MyApplication { public static void main(String[] args) { StringBuffer x = new StringBuffer(); x.append("Hello"); x.append("World"); String s = x.toString(); System.out.println(s); } }
In the above example, we have created an empty StringBuffer object,
StringBuffer x = new StringBuffer();
Now, in the next line, we have used the append() method of StringBuffer to insert Hello to the StringBuffer 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 StringBuffer to convert the StringBuffer 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) { StringBuffer x = new StringBuffer("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 StringBuffer object.
StringBuffer x = new StringBuffer("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) { StringBuffer x = new StringBuffer("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 StringBuffer object.
StringBuffer x = new StringBuffer("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) { StringBuffer x = new StringBuffer("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 StringBuffer object.
StringBuffer x = new StringBuffer("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) { StringBuffer x = new StringBuffer("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 StringBuffer class to reverse the String HelloWorld and make it dlroWolleH.
x.reverse();
And we got the below reversed String as output.