mjsr
mjsr

Reputation: 7590

How to break a string in lines

I'm confused with what is the correct way to break lines.

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

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

What it is the correct way?, these expressions always produce the same?

Upvotes: 5

Views: 898

Answers (6)

Kluyg
Kluyg

Reputation: 5347

If you want to support new-line characters for every platform (e.g. you need to parse input files, created under Linux/Windows/Mac in your ASP.NET web-site) and you do not carry about empty strings, I suggest to use this method instead:

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

This will return

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

for input string

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

Update: If you need to carry about empty lines, you can use

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

This should work for both "\r\n" and "\n" EOL charracter files.

Upvotes: 3

Yuriy Faktorovich
Yuriy Faktorovich

Reputation: 68717

Use

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

Environment.NewLine will pick the correct one for your operating system. This will fail if the data was created on a different system. Something that might work on all systems, but have some unintended consequences is

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

Some possible worrisome things is that it will remove all empty lines and split on carriage returns.

Upvotes: 12

NKCSS
NKCSS

Reputation: 2746

\r is a carriage return \n is a newline.

Windows uses \r\n by default (Environment.NewLine).

[Rewritten to clarify the Environment.NewLine part]

To get the correct characters to split your text on, you can use Environment.NewLine, which will report the correct characters based on your platform.

Upvotes: 2

Naraen
Naraen

Reputation: 3250

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

would be the way to do it.

The reason, both appear to give the same result is because, "\n" breaks the string after the "\r". So you have a substrings with a trailing "\r", which won't be obvious unless you look at it carefully with a hex editor or something.

That said I'd suggest using Environment.NewLine instead of "\r\n"

Upvotes: 0

Tony The Lion
Tony The Lion

Reputation: 63250

For reasons mentioned in other answers, only do what EDIT says. Both are fine, however personally I'd use:

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

EDIT:

USE Environment.Newline as suggested in other answers.

Upvotes: 0

Håvard
Håvard

Reputation: 10080

You can use Environment.NewLine to make sure you get the correct one.

Upvotes: 2

Related Questions