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

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


There is now:

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");    }}

Amended to follow the php spec for nl2br a little more closely (thanks Max for pointing that out). This still assumes \r\n new lines though...


All these answers are quite correct, but you should do a mystring.Replace("\r?\n", "<br />"); to catch UNIX line endings as well, if your source (user input or db) might deliver that.


I don't believe there's a 'stock standard function' to do this exactly the same as PHP's nl2br() function however the following will do the equivelant:

myString.Replace("\r\n", "<br />");