• We just launched and are currently in beta. Join us as we build and grow the community.

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

Supleese

Anime Poster Designer
S Rep
0
0
0
Rep
0
S Vouches
0
0
0
Vouches
0
Posts
124
Likes
167
Bits
2 MONTHS
2 2 MONTHS OF SERVICE
LEVEL 1 300 XP
Submitted by
planetsourcecode

on
Thu, 06/04/2009 - 02:21

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

 

437,153

314,794

314,803

Top