Replace “\r\n” in C#.

Once I was working on Stored Procedure, the output of SP was a string that I wanted to show on my page. This string has “\r\n” and I was thinking string will automatically break on “\r\n”. But I was wrong I didn’t. I tried to replace “\r\n” with ”
” tag but it didn’t word as well.

It was a simple problem but I got stuck. I scratched my head for hours and then I found a solution.

Original String:

 private void Replace()
    {
        string strPoem = “Baa, baa, black sheep,\r\n Have you any wool?\r\n Yes, sir, yes, sir,\r\n Three bags full;\r\n One for the master,\r\n And one for the dame,\r\n And one for the little boy\r\n Who lives down the lane.”;
        Literal1.Text = strPoem;
    }

1

Replaced String:

private void Replace()
    {
        string strPoem = “Baa, baa, black sheep,\r\n Have you any wool?\r\n Yes, sir, yes, sir,\r\n Three bags full;\r\n One for the master,\r\n And one for the dame,\r\n And one for the little boy\r\n Who lives down the lane.”;
        Literal2.Text = strPoem.Replace(Environment.NewLine, @”<br />”);
    }

2

Advertisements

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s