6

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"

Peter Mortensen
  • 30,738
  • 21
  • 105
  • 131
Slorthe
  • 101
  • 1
  • 1
  • 8
  • possible duplicate of [How can I remove "\r\n" from a string in c#? Can I use a regEx?](http://stackoverflow.com/questions/1981947/how-can-i-remove-r-n-from-a-string-in-c-can-i-use-a-regex) – Pierre-Luc Pineault Jul 15 '14 at 22:38
  • Use `string.replace()` – Rahul Jul 15 '14 at 22:38
  • 1
    do you assign result of `text.Replace()` call somewhere? It does not replace it in-place – zerkms Jul 15 '14 at 22:39
  • 2
    you must realize that strings are immutable,every "string method" you use returns to you a NEW string so you must assign to a string variable that be your text variable or a new one. – terrybozzio Jul 15 '14 at 22:43
  • Better solution has given here pl check https://stackoverflow.com/a/1982317/2208645 – Suraj Bhatt Dec 10 '20 at 07:05

7 Answers7

32

This reads better:

text = text.Replace(System.Environment.NewLine, string.Empty);
Peter Mortensen
  • 30,738
  • 21
  • 105
  • 131
Luke Hutton
  • 10,612
  • 6
  • 33
  • 58
5

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);
Peter Mortensen
  • 30,738
  • 21
  • 105
  • 131
  • 1
    Thank you for this code snippet, which might provide some limited, immediate help. A [proper explanation would greatly improve its long-term value](//meta.stackexchange.com/q/114762/206345) by showing _why_ this is a good solution to the problem, and would make it more useful to future readers with other, similar questions. Please [edit] your answer to add some explanation, including the assumptions you've made. – Suraj Rao Jan 15 '19 at 11:02
  • I had used this code in a web API where \r\n was not working. input: {\r\n \"LOINC\": [\r\n {\r\n \"LOINC_NUM\": \"10362-2\",\r\n \"COMPONENT\": \"Endomysium Ab.IgA\",\r\n \"PROPERTY\": \"PrThr\",\r\n \"TIME_ASPCT\": \"Pt\",\r\n }\r\n ]\r\n}; output: { "LOINC": [ { "LOINC_NUM": "10362-2", "COMPONENT": "Endomysium Ab.IgA", "PROPERTY": "PrThr", "TIME_ASPCT": "Pt" } ] } – Ramya Prakash Rout Jan 29 '19 at 11:50
  • The best answer so far – Alex Jun 19 '23 at 11:58
2

Are you setting the return value back to the variable?

text = text.Replace(@"\r\n", "");
gunr2171
  • 16,104
  • 25
  • 61
  • 88
ps2goat
  • 8,067
  • 1
  • 35
  • 68
2

It returns a value. You need to say text = ...

text = text.Replace(@"\r\n", "");
gunr2171
  • 16,104
  • 25
  • 61
  • 88
Alex
  • 266
  • 1
  • 6
1

Try this:

text = text.Replace(System.Environment.NewLine, "");
Peter Mortensen
  • 30,738
  • 21
  • 105
  • 131
raghu sathvara
  • 196
  • 1
  • 4
0

Try this.

text = text .Replace("\\r\\n", "");

It's work for me ;)

-1

You better try this.

text = text.Replace("\r\n", ""); 

Hope this work.

Zin Win Htet
  • 2,448
  • 4
  • 32
  • 54