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

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

    If there's always a single CRLF, then:

    myString = myString.Substring(0, myString.Length - 2);
    

    If it may or may not have it, then:

    Regex re = new Regex("\r\n$");
    re.Replace(myString, "");
    

    Both of these (by design), will remove at most a single CRLF. Cache the regex for performance.

提交回复
热议问题