Search code examples
c#arrayslist-separator

Append String array using TextInfo.ListSeparator


I am reading all lines from a CSV file. I wanted to get the specific line number of the and place it into an array by using Split. This is my current way:

var resultPath = GetFilePath();
String[] lines = null;
lines = System.IO.File.ReadAllLines(resultPath);
string[] values = lines[result.LineNumber - 1].Split(','); //Get specific line number and place in an array

Now, instead of this, I wanted to use TextInfo.LineSeparator.

Here is how I attempt:

var resultPath = GetFilePath();
String[] lines = null;
lines = System.IO.File.ReadAllLines(resultPath);
var listSeparator = System.Globalization.CultureInfo.CurrentCulture.TextInfo.ListSeparator;
string[] values = ??? //I am stuck here...

How do I resolve this?


Solution

  • var resultPath = GetFilePath();
    String[] lines = null;
    lines = System.IO.File.ReadAllLines(resultPath);
    var listSeparator = System.Globalization.CultureInfo.CurrentCulture.TextInfo.ListSeparator;
    string[] values = lines[result.LineNumber - 1].Split(new String[] { listSeparator }, StringSplitOptions.None); 
    

    Works Fine Now....