I have a textfile that looks like this :
John,Gauthier,blue,May
Henry,Ford,Red,June
James,Bond,Orange,December
I want to split it into a two dimensional string array so I could separate each lines then each words. Ex:
mystring[0][0] = "John"
mystring[1][3] = "June"
mystring[2][2] = "Orange"
Here's what I did right now:
string[] words = new string [100];
System.IO.StreamReader myfile = new System.IO.StreamReader("c:\\myfile.csv");
while (fichier.Peek() != -1)
{
i++;
words = myfile.ReadLine().Split(',');
}
I'm stuck. I'm able to split it into a one dimensional string array but not into a two dimensional string array. I guess I need to split it two times ; First time with '\n' and the second time with ',' and then put those two together.
Try this
Try:
Other idea, use a XML serilizer to serilize your hole Object. Two extensions for this:
Add than in any Static class and call:
or
This is actually a one-liner:
Since this is a beginner question, here's what's going on:
File.ReadLines(filename) returns a collection of all lines in your text file
.Select is an extension method that takes a function
s=>s.Split(',') is the function, it splits the string s by all commas and returns an array of strings.
.ToArray() takes the collection of string arrays created by .Select and makes an array out of that, so you get array of arrays.