Replace “\n” with “<BR\>”

We got some problem in storing newline character in database. This will help the developers in most of the development conditions.There are lot of times we accept user input in a textarea or a multiline textbox and save it to a database. But while displaying the data from the database, you will find that the line breaks are not displayed properly, infact there will be no line breaks. We will need to convert all the new line characters (”\n” character) to “
” so that they are properly rendered in the browser. There seems to be quite a few ways to do it but the simplest method which always worked for me is as follows. Just pass the string to the the function and it will format the string properly so that the new line characters are replaced with “
” tags public static string ConvertToBR(string InputString) { string Pattern = "\\n"; Regex Rgx = new Regex(Pattern); string OutputString = Rgx.Replace(InputString, "
"); return OutputString; }
About the author: PlanetSourceCode.in is a place for all developer providing free source codes, articles, complete projects,complete application in PHP, C/C++, Javascript, Visual Basic, Cobol, Pascal, ASP/VBScript, AJAX, SQL, Perl, Python, Ruby, Mobile Development

Add new comment