How do you append a new line(\n\r) character in StringBuilder
?
The AppendLine() method appends the content and add a new line on the end.
In Windows, a new line is denoted using “\r\n”, sometimes called a Carriage Return and Line Feed, or CRLF. Adding a new line in Java is as simple as including “\n” , “\r”, or “\r\n” at the end of our string.
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.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With