Converting Line Breaks from MsSQL using C#

April 21st, 2010

While working on a ASP.NET site, I needed to change the code in the codebehind pages in C# to convert \n to <br /> tags. I was working on a product review system for an ecommerce site, and when a user types in a review and presses enter, the line break didn’t convert once the review was outputted to the page.

Here’s a quick way I did it. Make a new regex pattern and then a for loop to loop through every row that your outputting. Then use the Replace method to swap it out for <br /> tags. Here’s a summary of the relavent code.


using System.Text.RegularExpressions;


Regex rgx = new Regex("\n");
//for loop code
outputString = rgx.Replace(inputString, "<br />");
//end for loop code

Similar Posts
Posted in Web Engineering

Comments