93

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

Peter Mortensen
  • 28,342
  • 21
  • 95
  • 123
user274364
  • 1,687
  • 1
  • 18
  • 26
  • 17
    That's not a newline under any common OS. Windows is \r\n, Mac is \r, Unix and Linux are \n. – Ben Voigt Apr 11 '10 at 16:39
  • 2
    Pedantically 'newline' is always '\n'. It was the platten shift character in mechanical teletypes. 'carriage return' is always '\r'. It was the carriage return character in teletypes. Each operating system may interpret them differently. In Unix/Linux the return is generally not used because it's unnecessary because it's no longer a mechanical system. – Jay Jan 09 '13 at 15:32
  • 6
    @Jay: A character causing only platen shift is not referred to as newline, but linefeed. – Ben Voigt Jul 12 '13 at 12:17

8 Answers8

127

I would make use of the 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();

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

Peter Mortensen
  • 28,342
  • 21
  • 95
  • 123
Adriaan Stander
  • 150,765
  • 27
  • 267
  • 275
  • 2
    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 Apr 11 '10 at 17:04
  • I have been trapped by append then appendline. I expected appendline to add a \n then my string, but appendline appends the string then the \n - http://msdn.microsoft.com/en-us/library/system.text.stringbuilder.appendline.aspx – Larry Nov 08 '10 at 08:11
  • 5
    Stringbuilder.AppendLine() does the same thing as sb.Append(Environment.NewLine); It is also neater I would say(http://msdn.microsoft.com/en-us/library/ebk18257.aspx) – Mulki Sep 12 '11 at 07:40
  • 3
    Be aware that the Environment.NewLine on unix systems is not "\r\n" so if you are implementing something specified as "\r\n" then you must write it in the code rather than calling AppendLine. – hultqvist Aug 31 '12 at 20:37
  • 1
    @hultqvist: Better to use `AppendLine` on a StreamWriter, then it will insert the newline sequence appropriate for that particular `StreamWriter` and not merely `Environment.NewLine`. For example, if you open a stream tied to an SMTP connection, you can set its `NewLine` property to "\r\n" and then you'll get valid SMTP handshake even on Unix. – Ben Voigt Jun 13 '17 at 16:36
  • 1
    AppendLine is simpler than this method. – Antonio Mano Dec 06 '18 at 17:29
  • Use AppendLine as mentioned in the answer by Lijo below – drewid Feb 23 '19 at 05:58
79

With the AppendLine method.

َََ

Peter Mortensen
  • 28,342
  • 21
  • 95
  • 123
Ben Voigt
  • 260,885
  • 36
  • 380
  • 671
25

Also, using the StringBuilder.AppendLine method.

Peter Mortensen
  • 28,342
  • 21
  • 95
  • 123
Fitzchak Yitzchaki
  • 8,867
  • 7
  • 51
  • 94
6

Use StringBuilder's append line built-in functions:

StringBuilder sb = new StringBuilder();
sb.AppendLine("First line");
sb.AppendLine("Second line");
sb.AppendLine("Third line");

Output

First line
Second line
Third line
Peter Mortensen
  • 28,342
  • 21
  • 95
  • 123
Lijo
  • 4,738
  • 1
  • 41
  • 55
5

It will append \n in Linux instead \r\n.

Himanshu Jansari
  • 28,446
  • 26
  • 101
  • 128
Kerry Jiang
  • 1,148
  • 13
  • 10
3
StringBuilder sb = new StringBuilder();

You can use sb.AppendLine() or sb.Append(Environment.NewLine);

Janmejay Kumar
  • 237
  • 2
  • 7
1

For multiple lines the best way I find is to do this:

        IEnumerable<string> lines = new List<string>
        {
            string.Format("{{ line with formatting... {0} }}", id),
            "line 2",
            "line 3"
        };
        StringBuilder sb = new StringBuilder();
        foreach(var line in lines)
            sb.AppendLine(line);

In this way you don't have to clutter the screen with the Environment.NewLine or AppendLine() repeated multiple times. It will also be less error prone than having to remember to type them.

user859400
  • 73
  • 2
  • 9
1

Just create an extension for the StringBuilder class:

Public Module Extensions
    <Extension()>
    Public Sub AppendFormatWithNewLine(ByRef sb As System.Text.StringBuilder, ByVal format As String, ParamArray values() As Object)
        sb.AppendLine(String.Format(format, values))
    End Sub
End Module
Peter Mortensen
  • 28,342
  • 21
  • 95
  • 123
Paul Ishak
  • 1,003
  • 13
  • 18