Removing carriage return and new-line from the end of a string in c#

Avik picture Avik · May 16, 2009 · Viewed 199.9k times · Source

How do I remove the carriage return character (\r) and the new line character(\n) from the end of a string?

Answer

RichieHindle picture RichieHindle · May 16, 2009

This will trim off any combination of carriage returns and newlines from the end of s:

s = s.TrimEnd(new char[] { '\r', '\n' });

Edit: Or as JP kindly points out, you can spell that more succinctly as:

s = s.TrimEnd('\r', '\n');