CRLF syntax single in C #

I have the following code:

string myTest = "Line1Test" + Environment.NewLine + "Line2Test" + Environment.NewLine + "Line3Test" + Environment.NewLine; string[] parseStr = myTest.Split(Environment.NewLine.ToCharArray()); 

What I get is the data in every other row in a new array. I think this is due to the fact that the dividing line is divided into both a line and a carriage return, but how can I get only one element per line?

+4
source share
3 answers
 string[] parseStr = myTest.Split( new string[] { Environment.NewLine }, StringSplitOptions.None ); 
+24
source

You can specify StringSplitOptions.RemoveEmptyEntries to remove empty entries.

 string[] parseStr = myTest.Split(Environment.NewLine.ToCharArray(), StringSplitOptions.RemoveEmptyEntries); 
+2
source

Another way may not be the best

 string myTest = "Line1Test" + Environment.NewLine + "Line2Test" + Environment.NewLine + "Line3Test" + Environment.NewLine; string[] parseStr = myTest.Split(Environment.NewLine.ToCharArray()).Where(i => i != string.Empty).ToArray(); 
-1
source

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


All Articles