How to split a line in lines

I am confused by what is the right way to break lines.

I read somewhere that windows use \ r \ n to break lines, but these two codes create the same

regex.split(sometext, "\r\n");
regex.split(sometext, "\n");

What is the right way ?, do these expressions always produce the same thing?

+5
source share
6 answers

If you want to support newline characters for each platform (for example, you need to parse input files created under Linux / Windows / Mac on your ASP.NET website) and you do not carry blank lines, I suggest using this method instead :

myString.Split(new char[] { '\n', '\r' }, StringSplitOptions.RemoveEmptyEntries)

It will return

["one","two","three"]

for input line

"one\r\ntwo\n\n\nthree"

Update: If you need to carry blank lines, you can use

myString.Replace("\r\n", "\n").Split("\n")

\ "\ r\n" "\n" EOL charracter.

+3

var myArray = sometext.Split(Environment.NewLine);

Environment.NewLine . , . -, ,

var myArray = sometext.Split(new[] {'\r', '\n'}, 
    StringSplitOptions.RemoveEmptyEntries);

, .

+12
+2

\ r - \n - .

Windows \r\n (Environment.NewLine).

[ Environment.NewLine]

, Environment.NewLine, .

+2

, , , EDIT. , :

regex.split(sometext, "\n");

EDIT:

Environment.Newline, .

0
regex.split(sometext, "\r\n");

- .

, , , "\n" "\ r". , "\ r", , - .

This suggests that I suggest using Environment.NewLine instead of "\ r \ n"

0
source

All Articles