开发者

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[] {',', '|'});

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜