Is there something like the nl2br () PHP function for ASP.NET MVC?

Is there a standard stock function that makes a new line for <br /> coding in ASP.Net MVC?

+4
source share
5 answers

Now it is:

 public static class StockStandardFunctions { public static string Nl2br(this string input) { return input.Nl2br(true); } public static string Nl2br(this string input, bool is_xhtml) { return input.Replace("\r\n", is_xhtml ? "<br />\r\n" : "<br>\r\n"); } } 

Modified to better follow php specification for nl2br (thanks Max for pointing this out). This still assumes \ n \ n newlines, though ...

+8
source

All of these answers are correct, but you should do mystring.Replace("\r?\n", "<br />"); to catch UNIX line endings if your source (user input or db) can provide this.

+4
source

I do not believe that the "standard stock" function for this is exactly the same as the PHP function nl2br (), but the following will do the same:

 myString.Replace("\r\n", "<br />"); 
+3
source
 mystring.Replace("\r\n","<br />"); 
+2
source

Sort of:

 public static string Nl2br(string str) { return Nl2br(str, true); } public static string Nl2br(string str, bool isXHTML) { string brTag = "<br>"; if (isXHTML) { brTag = "<br />"; } return str.Replace("\r\n", brTag + "\r\n"); } 

Here's the function signature from a PHP document:

string nl2br (string $ string [, bool $ is_xhtml = true])

The PHP function also adds a new line after the break tag.

+1
source

Source: https://habr.com/ru/post/1308367/


All Articles