Newline character in StringBuilder
How do you append a new line(\n\r) character in StringBuilder
?
Solution 1:
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.
Solution 2:
With the AppendLine method.
َََ
Solution 3:
Also, using the StringBuilder.AppendLine method.
Solution 4:
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
Solution 5:
It will append \n
in Linux instead \r\n
.