Parse TXT File Into 2D String Array in C#
I am looking for a way to parse a text file I have into a 2D String array with 9 rows and 7 columns. Every Pip should be another column and every Enter should be another row. 100|What color is the sky?|Blue,Red,Green,Orange|Blue
Here is the code I have so far but I don't know how to correctly parse it.
private void loadQuestions()
{
string line;
string[,] sQuestionArray = new string[9, 7];
开发者_运维技巧System.IO.StreamReader file = new System.IO.StreamReader("questions.txt");
while ((line = file.ReadLine()) != null)
{
}
file.Close();
}
Any help would be greatly appreciated.
If you can use string[][]
instead of string[,]
then you can do
string[] lines = File.ReadAllLines("questions.txt");
string[][] result = lines.Select(l => l.Split(new []{'|', ','})).ToArray();
Take a look at Split.
Ex: var splitLine=line.Split(new[] {',', '|'});
精彩评论