从C#中的字符串末尾删除回车符和换行符

问题描述:

如何从字符串末尾删除回车符(\r)和换行符(\n)?

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

这将从s的末尾修剪掉回车符和换行符的任何组合:

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

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

编辑:或者,JP指出,您可以更简洁地将其拼写为:

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

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