1

I have a problem with converting \n to < br/>. The text I want to convert is test\ntest\n\ntest I want it to be like test<br />test<br /><br />test. So every \n needs to be replaced with <br />.

Now I'm using this code:

Regex regex = new Regex(@"(\n)+");
string ticketCategorieOmschrijving = regex.Replace("test\ntest\n\ntest", "<br />");

But this code replaces \n\n to a single <br />, while it needs to be <br /><br />

Can someone help?

Arman Ebrahimpour
  • 4,252
  • 1
  • 14
  • 46
  • 3
    Why go the Regex route when you can just use [String.Replace()](https://learn.microsoft.com/en-us/dotnet/api/system.string.replace?view=netcore-3.1#System_String_Replace_System_String_System_String_)? `"test\ntest\n\ntest".Replace("\n", "
    ");`
    – Simon Wilson Jun 03 '20 at 10:19
  • I'd suggest using CSS to solve this https://stackoverflow.com/a/2703609/34092 . – mjwills Jun 03 '20 at 11:00

1 Answers1

5

You can simply use String Replace method:

str = str.Replace("\n", "<br />");
Arman Ebrahimpour
  • 4,252
  • 1
  • 14
  • 46