CRLF parsing blues in C#
我有以下代码:
1 2 3 4 | string myTest ="Line1Test" + Environment.NewLine + "Line2Test" + Environment.NewLine + "Line3Test" + Environment.NewLine; string[] parseStr = myTest.Split(Environment.NewLine.ToCharArray()); |
我得到的是新数组中每隔一行的数据。我认为这是因为拆分行对换行和回车都进行了拆分,但如何才能为每行只获取一个元素?
1 2 3 4 |
您可能需要指定
1 2 | string[] parseStr = myTest.Split(Environment.NewLine.ToCharArray(), StringSplitOptions.RemoveEmptyEntries); |
号
不过,还有一种方法可能不是最好的
1 2 3 4 5 | string myTest ="Line1Test" + Environment.NewLine + "Line2Test" + Environment.NewLine + "Line3Test" + Environment.NewLine; string[] parseStr = myTest.Split(Environment.NewLine.ToCharArray()).Where(i => i != string.Empty).ToArray(); |