Normalize newlines in C#

前端 未结 8 2055
滥情空心
滥情空心 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:19

    Normalise breaks, so that they are all \r\n

    var normalisedString =
                sourceString
                .Replace("\r\n", "\n")
                .Replace("\n\r", "\n")
                .Replace("\r", "\n")
                .Replace("\n", "\r\n");
    

提交回复
热议问题