Normalize newlines in C#

前端 未结 8 2064
滥情空心
滥情空心 2020-12-03 06:48

I have a data stream that may contain \\r, \\n, \\r\\n, \\n\\r or any combination of them. Is there a simple way to normalize the data to make all of them simply become \\r

8条回答
  •  借酒劲吻你
    2020-12-03 07:23

    I'm with Jamie Zawinski on RegEx:

    "Some people, when confronted with a problem, think "I know, I’ll use regular expressions." Now they have two problems"

    For those of us who prefer readability:

    • Step 1

      Replace \r\n by \n

      Replace \n\r by \n (if you really want this, some posters seem to think not)

      Replace \r by \n

    • Step 2 Replace \n by Environment.NewLine or \r\n or whatever.

提交回复
热议问题