使用 TextInfo.ListSeparator 追加字符串数组
Append String array using TextInfo.ListSeparator
我正在读取 CSV 文件中的所有行。我想获取的特定行号,并使用 Split 将其放入数组中。这是我目前的方式:
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
现在,我想使用 TextInfo.LineSeparator
。
而不是这个
我是这样尝试的:
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...
我该如何解决?
根据你现在的方式:
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(listSeparator); //you can change it directly with your old separator ','
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);
现在工作正常....
我正在读取 CSV 文件中的所有行。我想获取的特定行号,并使用 Split 将其放入数组中。这是我目前的方式:
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
现在,我想使用 TextInfo.LineSeparator
。
我是这样尝试的:
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...
我该如何解决?
根据你现在的方式:
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(listSeparator); //you can change it directly with your old separator ','
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);
现在工作正常....