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

后端 未结 12 1154
终归单人心
终归单人心 2020-12-02 13:57

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

12条回答
  •  情书的邮戳
    2020-12-02 14:26

    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');
    

提交回复
热议问题