Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Newline character in StringBuilder

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

like image 277
user274364 Avatar asked Apr 11 '10 16:04

user274364


People also ask

Which method appends a newline character to the end of string?

The AppendLine() method appends the content and add a new line on the end.

How do you append to next line in Java?

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.


1 Answers

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.

like image 133
Adriaan Stander Avatar answered Oct 29 '22 17:10

Adriaan Stander