I am reading in a text file into my program. I have managed to do the following

1. Split first line of the text file up based on the position of the commas and put them into an array. I.E

StreamReader reader = new StreamReader("shopping.txt");
            string line = reader.ReadLine();
string[] elements= line.Split(',');

2. I have also managed to split each line of the text file into an array.

string record = reader.ReadToEnd();
            string test = record.Replace(',','\t');
            string[] creturn = test.Split('\n');

My problem is however that I am unable to make an array of creturns[] on lines that already have an array of elements[].

I would appreciate any assistance you may be able to give me.

5 Years
Discussion Span
Last Post by Momerath

You can create an array of arrays like this:

int[] list1 = new int[4] { 1, 2, 3, 4};
int[] list2 = new int[4] { 2, 3, 4, 5};
int[] list3 = new int[4] { 1, 3, 2, 1 };
int[] list4 = new int[4] { 5, 4, 3, 2 };

int[][] lists = new int[][] {  list1 ,  list2 ,  list3 ,  list4  };

I've tried this

string[] creturn = new string[]{ element[0], element[1], element[2], element[3],element[4] };
            Console.WriteLine("{0}", creturn[0]);

However I only get the first element printed to the screen as opposed to the whole line?. If I initialise the new string as

new string[4]

, it says an array initialiser of length 4 is expected?


Can you just do all splits simultaneously when you ReadToEnd() ?

using System;
using System.IO;

namespace DW_396057
   class Program
      static void Main(string[] args)
         StreamReader fileIn = new StreamReader("c:/science/TomDigs.txt");
         string[] arr_strData = fileIn.ReadToEnd().Split("\n\r,\t".ToCharArray(), StringSplitOptions.RemoveEmptyEntries);
String[] lines = File.ReadAllLines("c:/science/TomDigs.txt");
String[][] data = new String[lines.Length][];
for (int i = 0; i < lines.Length; i++) {
    data[i] = lines[i].Split(',');
This article has been dead for over six months. Start a new discussion instead.
Have something to contribute to this discussion? Please be thoughtful, detailed and courteous, and be sure to adhere to our posting rules.