C# – the easiest way in C# to trim a newline off of a string


I want to make sure that _content does not end with a NewLine character:

_content = sb.ToString().Trim(new char[] { Environment.NewLine });

but the above code doesn't work since Trim seems to not have an overloaded parameter for a collection of strings, only characters.

What is the simplest one-liner to remove an Enivronment.Newline from the end of a string?

Best Solution

The following works for me.

sb.ToString().TrimEnd( '\r', '\n' );


sb.ToString().TrimEnd( Environment.NewLine.ToCharArray());