tags:

views:

146

answers:

6

talt text

it returns not what i expected. i expected something like: ab cab ab

what am i doing wrong?

+6  A: 

Since you are splitting on "\r" and "n", String.Split extracts the empty string from "\r\n".

Take a look at StringSplitOptions.RemoveEmptyEntries or use new String[] { "\r\n" } instead of "\r\n".ToCharArray().

Jens
+7  A: 

don't do .ToCharArray()

it will split \r then \n

that why you have empty value

something like this should work

var aa = ("a" & Environment.NewLine & "b" & Environment.NewLine & "c").Split(New String[] {Environment.NewLine}, StringSplitOptions.RemoveEmptyEntries);
Fredou
+1  A: 

You just splitting the string using \r or \n as delimiters, not the \r\n together.

Shark
A: 

This option also works, string [] b = Regex.Split(abc, "\r\n");

srinivas reddy thatiparthy
+1  A: 

Environment.NewLine is probably the way to go but if not this works

var ab = "a\r\nb\r\nc";
var abs = ab.Split(new[]{"\r\n"}, StringSplitOptions.None);
Jonas Elfström
A: 

My understanding is that the string char sequence you provide to the Split method is a list of delimiter characters, not a single delimiter madeof several characters.

In your case, Split consider the '\r' and '\n' characters as delimiters. So when it encounters the '\r\n' sequence, it returns the string between those 2 delimiters, an empty string.

Mr_Hic-up