Newline character in StringBuilder
Asked Answered
C

8

107

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

Calbert answered 11/4, 2010 at 16:34 Comment(3)
That's not a newline under any common OS. Windows is \r\n, Mac is \r, Unix and Linux are \n.Disavow
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.Ocrea
@Jay: A character causing only platen shift is not referred to as newline, but linefeed.Disavow
P
138

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.

Posology answered 11/4, 2010 at 16:36 Comment(7)
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.Disavow
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 - msdn.microsoft.com/en-us/library/…Forlini
Stringbuilder.AppendLine() does the same thing as sb.Append(Environment.NewLine); It is also neater I would say(msdn.microsoft.com/en-us/library/ebk18257.aspx)Ferdelance
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.Assuming
@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.Disavow
AppendLine is simpler than this method.Dependency
Use AppendLine as mentioned in the answer by Lijo belowCharlatanry
D
83

With the AppendLine method.

َََ

Disavow answered 11/4, 2010 at 16:39 Comment(1)
This would be nice if it had the same number of overloads as Append(). Looks like an oversight.Escalante
G
27

Also, using the StringBuilder.AppendLine method.

Gallicism answered 11/4, 2010 at 16:39 Comment(0)
S
14

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
Scyphus answered 26/7, 2016 at 8:7 Comment(0)
H
6

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

Helminthology answered 5/1, 2012 at 7:4 Comment(1)
My application is running in Linux but I want it return \r\n and I am currently using stringbuilder.AppendLine.Holeproof
T
4
StringBuilder sb = new StringBuilder();

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

Theobald answered 11/3, 2019 at 8:15 Comment(0)
G
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.

Girth answered 1/5, 2015 at 1:56 Comment(0)
R
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
Revanche answered 25/7, 2018 at 14:9 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.