0

I'm trying to format a user input (multi row) in a application. The text that's returned could be like this:

"This is \r\na test\r\nwith rowbreaks"

This later is sent by email and the text just formats like this:

"This is a test with rowbreaks"

How can I format this to HTML? I've tried HttpUtility.HtmlEncode, but that returns just the same with \r\n.

Robin
  • 740
  • 3
  • 12
  • 30

1 Answers1

2

You need <br /> tags for HTML linebreaks, all whitespace gets replaced by spaces. You can easily do this manually:

var text = "This is \r\na test\r\nwith rowbreaks";
var htmlText = text.Replace(System.Environment.NewLine, "<br />");

Or using other replacement methods as explained in: Replace Line Breaks in a String C#

j4nw
  • 2,227
  • 11
  • 26