Remove "new lines" from string variable? C#

Hi.

I have a simple string variable with multiple lines, so I’m trying to make all the lines into one single line.

I have tried this code, but it doesn’t work:

transform.guiText.text = transform.guiText.text.Replace(System.Environment.NewLine, “”);

How is this possible?

Best regards,
Andreas.

The simplest way would be to do multiple calls to replace to remove both the carriage return and line feed characters individually, as you probably don’t really care which order they were in, and just want rid of them.

newString = oldString.Replace(“\r”, “”).Replace("
", “”);

I’m not going to suggest that this is the best way of doing it, but it is at least straightforward.

Remove new line from a string

myString.Replace(System.Environment.NewLine, “replacement text”)

or

Regex.Replace(str, @"
\r", “”);

Or

str.Replace("
“, String.Empty);
str.Replace(”\r", String.Empty);
str.Replace(" ", String.Empty);

Remove space from a string

xyz.Replace(" ", string.empty);

Full Source…Remove new line from a string

Dov