我试图在应用程序中格式化用户输入(多行)。返回的文本可能是这样的:
"This is \r\na test\r\nwith rowbreaks"
以后通过电子邮件发送,文本格式如下:
"This is a test with rowbreaks"
如何将其格式化为HTML?我已经尝试过HttpUtility.HtmlEncode,但返回与\ r \ n一样的。
答案 0 :(得分:2)
HTML换行符需要<br />
个标记,所有空格都会被空格替换。您可以轻松地手动执行此操作:
var text = "This is \r\na test\r\nwith rowbreaks";
var htmlText = text.Replace(System.Environment.NewLine, "<br />");
或使用其他替换方法,如下所述: Replace Line Breaks in a String C#