Java – How to convert String to StringBuffer and vice-versa ?

In this article, we will discuss how to convert String to StringBuffer and vice-versa

Note: String to StringBuilder conversion is also possible, read StringBuilder to String conversion and vice-versa

String to StringBuffer and vice-versa :

  1. String to StringBuffer using append() method
  2. StringBuffer to String using toString() method

Let us move forward and discuss above conversions

1. String to StringBuffer using append() method :

  • For converting String to StringBuffer, we can use append() method of StringBuffer class
  • Method signature:
public synchronized StringBuffer append(String str);

ConvertStringIntoStringBufferUsingAppendMethod.java

package in.bench.resources.string.to.stringbuffer;

public class ConvertStringIntoStringBufferUsingAppendMethod {

	public static void main(String[] args) {

		// String - 1
		String str1 = "This is Java Weblog. ";

		// create StringBuffer object
		StringBuffer sb = new StringBuffer();

		// 1. convert String to StringBuffer
		// using append() method
		sb.append(str1);

		// String - 2
		String str2 = "And there are over 500+ articles on Java.";

		// 2. again, convert String-2 to StringBuffer
		// using append() method
		sb.append(str2);

		// String - 3
		String str3 = "Covering most of the Core Java topics.";

		// 3. third time, convert String-3 and
		// add newline '\n' using + operator
		sb.append("\n" + str3);

		// print to console
		System.out.println("Ex: String to StringBuffer"
				+ " using append() method : \n\n" + sb);
	}
}

Output:

Ex: String to StringBuffer using append() method : 

This is Java Weblog. And there are over 500+ articles on Java.
Covering most of the Core Java topics.

2. StringBuffer to String using toString() method :

  • For converting StringBuffer to String, we can use toString() method of String class
  • Method signature:
public synchronized String toString();

ConvertStringBufferIntoStringUsingToStringMethod.java

package in.bench.resources.stringbuffer.to.string;

public class ConvertStringBufferIntoStringUsingToStringMethod {

	public static void main(String[] args) {

		// create StringBuffer object
		StringBuffer sb = new StringBuffer();

		// 1. append some string values
		sb.append("Google is top search-engine. ");

		// 2. again, append some more string values
		sb.append("To get latest topics on Core Java.");

		// 3. third time, append String-3 and
		// add newline '\n'
		sb.append("\nAnd it can search contents in real-time.");

		// convert StringBuffer to String using toString() method
		String str = sb.toString();

		// print to console
		System.out.println("Ex: StringBuffer to String"
				+ " using toString() method: \n\n" + str);
	}
}

Output:

Ex: StringBuffer to String using toString() method: 

Google is top search-engine. To get latest topics on Core Java.
And it can search contents in real-time.

References:

Happy Coding !!
Happy Learning !!

Java - How to convert String to StringBuilder and vice-versa ?
Short to String conversion in Java - 5 ways