How do you append a new line(\n\r) character in StringBuilder
?
Newline character in StringBuilder
Asked Answered
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
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.
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 below –
Charlatanry
With the AppendLine method.
َََ
This would be nice if it had the same number of overloads as Append(). Looks like an oversight. –
Escalante
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
It will append \n
in Linux instead \r\n
.
My application is running in Linux but I want it return \r\n and I am currently using stringbuilder.AppendLine. –
Holeproof
StringBuilder sb = new StringBuilder();
You can use sb.AppendLine() or sb.Append(Environment.NewLine);
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.
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
© 2022 - 2024 — McMap. All rights reserved.