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

后端 未结 12 1128
终归单人心
终归单人心 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:25

    s.TrimEnd();
    

    The above is all I needed to remove '\r\n' from the end of my string.

    The upvoted answer seems wrong to me. Firstly, it didn't work when I tried, secondly, if it did work I would expect that s.TrimEnd('\r', '\n') would only remove either a '\r' or a '\n', so I'd have to run it over my string twice - once for when '\n' was at the end and the second time for when '\r' was at the end (now that the '\n' was removed).

提交回复
热议问题