views:

521

answers:

3

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

+10  A: 

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.

astander
It's possible to mix Append and AppendLine to get new lines where you want them, not after every Append. Also, I linked to the zero-argument version of AppendLine which appends nothing but the newline sequence.
Ben Voigt
+10  A: 

with the AppendLine method

Ben Voigt
+2  A: 

also, using StringBuilder.AppendLine method.

Mendy
great answer ;)
Ben Voigt