Illep Illep - 2 months ago 14
Java Question

How to append a newline to StringBuilder

I have a

object,

StringBuilder result = new StringBuilder();
result.append(someChar);


Now I want to append a newline character to the
StringBuilder
. How can I do it?

result.append("/n");


Does not work. So, I was thinking about writing a newline using Unicode. Will this help? If so, how can I add one?

Answer

It should be

r.append("\n");

But I recommend you to do as below,

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

System.getProperty("line.separator") gives you system-dependent newline in java. Also from Java 7 there's a method that returns the value directly: System.lineSeparator()