Thursday, May 20, 2021

Buggish: C# Replacing \n with
versus \r\n versus \n\n

Problem to be solved

I need to replace MS SQL break lines with HTML break lines so that it can be displayed on a web page.

Background

Our network group decided to make routers reject posts that are too big. I have a tool to compare database objects across multiple environments to validate and to promote. I used a text box to display the sql queries. If there were five environments, then there would be five textboxes. If the stored procedure was really large, then I would have it five times. So when I post back to the server, I could potentially return a lot of data.

I don't want to truncate because most new changes are at the bottom. I don't want to skim because are at the top. And fixes are typically in the middle.

So decided to use html <pre> tag. This requires me to convert new lines to html new lines. I'm usually lazy and just convert \n to <br/> but then I realized that this returned two break lines which caused a blank line in between every line. This was a pain because that means it took me twice as long to skim through the code.

For some odd reason, I felt like doing both a \n\n and \r\n replace and found this did the same... even though the string value was just \r\n. This is really odd. Although works for me, this is not what I expected.


Final decision

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

No comments:

Post a Comment