How to append new line to StringBuffer in Java

In this article, we will discuss how to append newline to StringBuffer

There are ways to append newline to StringBuffer or StringBuilder object, we will list down below

  1. Using System.getProperty(“line.separator”); which works with all Java versions
  2. Using System.lineSeparator(); which is introduced in Java 1.7 version
  3. Use “\n” for appending newline, but it is NOT recommend as it is doesn’t work with all platforms and also sometimes we need to escape back-slash (\) character
  4. Note: 1st two approaches are recommended as it works well with all platforms

 

Example 1: To append newline to StringBuffer

Method Signature:

public StringBuffer append(String str);

Syntax:

sbuffer.append(System.getProperty("line.separator"));

AppendNewLineToStringBuffer.java

package in.bench.resources.sb.operation;

public class AppendNewLineToStringBuffer {

	public static void main(String[] args) {

		// create new StringBuffer object
		StringBuffer sbuffer = new StringBuffer(
				"Popular frameworks in Java are:");

		// 1. append 1st line-break
		sbuffer.append(System.getProperty("line.separator"));

		// 2. add 1st framework-name and then 2nd line-separator
		sbuffer.append("Spring");
		sbuffer.append(System.getProperty("line.separator"));

		// 3. add 2nd framework-name and then 3rd line-separator
		sbuffer.append("Hibernate");
		sbuffer.append(System.getProperty("line.separator"));

		// add 3rd framework-name
		sbuffer.append("Struts");

		// pretty print output to console
		System.out.println(sbuffer.toString());
	}
}

Output

Popular frameworks in Java are:
Spring
Hibernate
Struts

Note:

  • In a very similar way, line-break can also be appended to StringBuilder object
  • We will use new version of line-break i.e.; System.lineSeparator(); instead of System.getProperty(“line.separator”); starting from Java 1.7 version

 

We will execute a demo program for StringBuilder as well in the below example

Example 2: To append line-break to StringBuilder

Method Signature:

public StringBuilder append(String str);

Syntax:

sbuilder.append(System.lineSeparator());

AppendNewLineToStringBuilder.java

package in.bench.resources.sb.operation;

public class AppendNewLineToStringBuilder {

	public static void main(String[] args) {

		// create new StringBuilder object
		StringBuilder sbuilder = new StringBuilder(
				"Important topics in Java for Interview are:");

		// 1. append 1st line-break
		sbuilder.append(System.lineSeparator());

		// 2. add 1st framework-name and then 2nd line-separator
		sbuilder.append("Collection");
		sbuilder.append(System.lineSeparator());

		// 3. add 2nd framework-name and then 3rd line-separator
		sbuilder.append("Multi-threading");
		sbuilder.append(System.lineSeparator());

		// add 3rd framework-name
		sbuilder.append("Serialization");

		// pretty print output to console
		System.out.println(sbuilder.toString());
	}
}

Output

Important topics in Java for Interview are:
Collection
Multi-threading
Serialization

Note:

  • In first StringBuffer example, we have used System.getProperty(“line.separator”); to append newline to StringBuffer object. And this works well with all Java versions
  • Whereas in the second example, we are using  System.lineSeparator(); which works with latest Java version starting from Java 1.7 or higher version

 

Hope, you found this article very helpful. If you have any suggestion or want to contribute any other way or tricky situation you faced during Interview hours, then share with us. We will include that code here.

 

References:

 

Happy Coding !!
Happy Learning !!

Difference between capacity() and length() methods of StringBuffer in Java
How to clear or delete StringBuffer contents in Java