user274364 user274364 - 3 months ago 25
C# Question

Newline character in stringbuilder

How do you append new line(\n\r) character in

StringBuilder
?

Answer

I would make use of Environment.NewLine Property

Something like

StringBuilder sb = new StringBuilder();
sb.AppendFormat("Foo{0}Bar", Environment.NewLine);
string s = sb.ToString();

Or

StringBuilder sb = new StringBuilder();
sb.Append("Foo");
sb.Append("Foo2");
sb.Append(Environment.NewLine);
sb.Append("Bar");
string s = sb.ToString();

EDIT:

If you wish to have a new line after each append, you can have a look at @Ben Voigt answer.