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
It's a two step process.
First you convert all the combinations of \r
and \n
into a single one, say \r
Then you convert all the \r
into your target \r\n
normalized =
original.Replace("\r\n", "\r").
Replace("\n\r", "\r").
Replace("\n", "\r").
Replace("\r", "\r\n"); // last step
I agree Regex is the answer, however, everyone else fails to mention Unicode line separators. Those (and their variations with \n) should be included.