Cistoran
Cistoran

Reputation: 1597

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.

Upvotes: 0

Views: 1154

Answers (2)

Daniel Mann
Daniel Mann

Reputation: 59020

Take a look at Split.

Ex: var splitLine=line.Split(new[] {',', '|'});

Upvotes: 0

Bala R
Bala R

Reputation: 108957

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();

Upvotes: 2

Related Questions