Dean Forant
Dean Forant

Reputation: 61

appending Line Breaks in StringBuilder

I'm creating a online RSVP web app using MVC and C#. One of the requirements is to create and send a confirmation email to the person upon verifying that they are on the guest list.

Everything works well but I'm using StringBuilder to build out my email body text. The one think I need is Line Breaks so I can display some text in an address Block style. for example.

Dean Forant
Phone: 555-555-5555
Email: [email protected]

When I use appendLine("text") The Text appends but the next line appears on the same line in the email

Belows is a sample of the rendered output in the email message

Dean Forant: Phone – 555-555-555 Email – [email protected]

I've tried adding an additional AppendLine(); between the each line but that gives me a line space which I don't want.

Based on some of the other articles I have read on here, I tried to do the following:

AppendFormat("text{0}",Environment.NewLine);

but that seem to have no effect. Meaning, it still show all the lines on one line.

Does anyone have any suggestions?

Thanks, Dean

Upvotes: 1

Views: 4948

Answers (1)

randomstudious
randomstudious

Reputation: 122

You can use StringBuilder's Append() method to add a new line like this:

StringBuilder sb = new StringBuilder();

sb.AppendLine("Dean Forant");
sb.Append(Environment.NewLine);
sb.AppendLine("Phone: 555-555-5555");
sb.Append(Environment.NewLine);
sb.AppendLine("Email: [email protected]");

Upvotes: 1

Related Questions