将 csv 文件中的字符串转换为多维数组
strings from csv file into multidimensional array
现在我尝试将数据从 csv 文件转换为多维数组。这些数据是 DOS 控制台应用程序(主要是文本)中可玩角色的一些简单属性。该文件是这样构建的
"name; gender; role; hp; selectiontoken"
有 6 个不同的角色(每个角色两个)。每个字符应该是数组中的一行,每个属性应该是一列。
static void Main(string[] args)
{
string[,] values= new string [5,6];
var reader = new StreamReader(File.OpenRead(@"C:\path"));
while (!reader.EndOfStream)
{
string line = reader.ReadLine();
values = line.Split(';'); //Visual C# 2010 Express gives me an error at this position "an implicated conversion between string [] and string [,] is not possible"
}
我的问题是,如何将 "line" 中的列拆分为 "values"?
就像蒂姆向你解释的那样,String.Split return string[]。如果需要,您可以将其存储在 List<string[]>
中。
static void Main(string[] args)
{
List<string[]> values= new List<string[]>();
var reader = new StreamReader(File.OpenRead(@"C:\path"));
while (!reader.EndOfStream)
{
string line = reader.ReadLine();
values.Add(line.Split(';'));
}
}
现在我尝试将数据从 csv 文件转换为多维数组。这些数据是 DOS 控制台应用程序(主要是文本)中可玩角色的一些简单属性。该文件是这样构建的
"name; gender; role; hp; selectiontoken"
有 6 个不同的角色(每个角色两个)。每个字符应该是数组中的一行,每个属性应该是一列。
static void Main(string[] args)
{
string[,] values= new string [5,6];
var reader = new StreamReader(File.OpenRead(@"C:\path"));
while (!reader.EndOfStream)
{
string line = reader.ReadLine();
values = line.Split(';'); //Visual C# 2010 Express gives me an error at this position "an implicated conversion between string [] and string [,] is not possible"
}
我的问题是,如何将 "line" 中的列拆分为 "values"?
就像蒂姆向你解释的那样,String.Split return string[]。如果需要,您可以将其存储在 List<string[]>
中。
static void Main(string[] args)
{
List<string[]> values= new List<string[]>();
var reader = new StreamReader(File.OpenRead(@"C:\path"));
while (!reader.EndOfStream)
{
string line = reader.ReadLine();
values.Add(line.Split(';'));
}
}