Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How do I remove \r\n from string in C#?

Tags:

string

c#

I am trying to figure out an easy way remove \r\n from a string.

Example: text = "this.is.a.string.\r\nthis.is.a.string\r\n"

I tried:

text.Replace("\r\n", "") and text.Replace("\r\n", string.Empty), but it doesn't work. \r\n is still in the string...

The result should be: "this.is.a.string.this.is.a.string"

like image 824
Slorthe Avatar asked Jul 15 '14 22:07

Slorthe


2 Answers

This reads better:

text = text.Replace(System.Environment.NewLine, string.Empty);
like image 161
Luke Hutton Avatar answered Oct 26 '22 22:10

Luke Hutton


Strings in .NET are immutable and so you cannot change an existing string - you can only create a new one. The Replace method returns the modified result, i.e.

text = text.Replace(System.Environment.NewLine, string.Empty);
text = JObject.Parse(text);
like image 43
Ramya Prakash Rout Avatar answered Oct 26 '22 23:10

Ramya Prakash Rout