0

I'm sorry if this has been answered before but I couldn't find the answer which fit my needs

I have a richTextBox in c# and I want to get a continuous string of text of the contents

The problem is .Text will get something like:

First line \n Second Line \n Third Line

and what I need is:

First line Second Line Third Line

I don't want to directly parse the \n as it might be relevant to the text I'm retrieving but I don't want it added based on the lines of the richTextBox

Any ideas? Any help is gratefully appreciated.

Gerak
  • 71
  • 1
  • 5

1 Answers1

1

If I understand correctly, would this work for you?

string text = richTextBox.Text.Replace(Environment.NewLine, " ");

The alternative could be to use the regex from this answer.

string text = Regex.Replace(richTextBox.Text, @"\r\n?|\n", " ");
Community
  • 1
  • 1
keyboardP
  • 68,824
  • 13
  • 156
  • 205
  • its close, but that returns: First Line\Second Line\Third Line – Gerak Jun 29 '13 at 00:48
  • You can replace with a `space` instead of an empty string (I've updated the answer). Or do you mean there are actual backslashes in the output? – keyboardP Jun 29 '13 at 00:51
  • Yes there are slashes in the output, although maybe thats an error in judgement by reading the VS Locals window? – Gerak Jun 29 '13 at 00:55
  • Possibly but I can't test it at the moment. However, [this post](http://stackoverflow.com/a/8196219/187697) uses a regex to remove the new lines so you can try that to see if that's the issue. I've updated my answer to include that. – keyboardP Jun 29 '13 at 01:03