Question about line splitting

t alt text http://i43.tinypic.com/2rpfjth.png

it returns not what I expected. I expected something like: ab taxi AB

what am I doing wrong?

+4
source share
6 answers

do not execute .ToCharArray ()

it breaks \ r, then \ n

why do you have an empty value

something like this should work

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

Since you split into "\ r" and "n", String.Split extracts an empty string from "\ r \ n".

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

+6
source

You simply split the string using \r or \n as delimiters, rather than \r\n together.

+1
source

Environment.NewLine is probably the way to go, but if that doesn't work

 var ab = "a\r\nb\r\nc"; var abs = ab.Split(new[]{"\r\n"}, StringSplitOptions.None); 
+1
source

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

0
source

I understand that the char sequence that you provide to the Split method is a list of delimiter characters, not a single delimiter consisting of several characters.

In your case, Split treats the characters '\ r' and '\ n' as delimiters. Therefore, when it encounters the sequence "\ r \ n", it returns a string between the two delimiters, an empty string.

0
source

Source: https://habr.com/ru/post/1305660/


All Articles