0

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.

3
  • When you say 7 columns, do you mean that commas should also be treated as separate columns as well? IE - Row 1 should be 100 - What color is the sky? - Blue - Red - Green - Orange - Blue ? Commented Oct 7, 2011 at 20:15
  • Given your example, how would you expect your array to be structured? (example) Commented Oct 7, 2011 at 20:16
  • Array should be like [100,What color is the sky,Blue,Red,Green,Orange,Blue] then there are 9 rows similar to that but the contents of each column vary. Commented Oct 7, 2011 at 20:24

2 Answers 2

2

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();
Sign up to request clarification or add additional context in comments.

3 Comments

Just trying to figure out how yours works. I assume it stores the information in string [][] result but how do I call the values? I tried calling result[1][1]; but I just get an error "Index was outside bounds of the array."
@Cistoran can you post a few lines of sample data? with just one line, you could try result[0][1] I guess.
Oh that works excellent, forgot that arrays start at 0 and not 1. Thanks!
0

Take a look at Split.

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

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.