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

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

    This was too easy -- for me I'm filtering out certain email items. I'm writing my own custom email junk filter. With \r and/or \n in the string it was wiping out all items instead of filtering.

    So, I just did filter = filter.Remove('\n') and filter = filter.Remove('\r'). I'm making my filter such that an end user can use Notepad to directly edit the file so there's no telling where these characters might embed themselves -- could be other than at the start or end of the string. So removing them all does it.

    The other entries all work but Remove might be the easiest?

    I learned quite a bit more about Regex from this post -- pretty cool work with its use here.

提交回复
热议问题