Click here to Skip to main content
15,940,430 members
Please Sign up or sign in to vote.
1.00/5 (1 vote)
See more:
So here's the thing,
I have a Textbox with this format:
1,Goku
2,Krillin
3,Friezer
and so on...

and I have an array like this:
array[,] arrDBZ;

I want that for every line in the text file populate my array for example:
arrDBZ would have this values:
arrDBZ[1,Goku]
arrDBZ[2,Krillin]
arrDBZ[3,Friezer]
and so on...

How can i do this?
So far I have this code I found on google:
C#
using (OpenFileDialog fdText=new OpenFileDialog())
            {
                fdText.Filter="Text Files (*.txt)|*.txt";
                fdText.InitialDirectory = Environment.SpecialFolder.MyDocuments.ToString();
                fdText.Multiselect = false;
                fdText.Title = "SELECT TEXT FILE";
                if (fdText.ShowDialog() == DialogResult.OK)
                {
                    string data = System.IO.File.ReadAllText(fdText.FileName);
                    string[,] parsedData;

                    var res = data.Split(',')
                    .Select(p => Regex.Split(p, "(?<=\\G.{2})"))
                    .ToArray();

                    var twoD = new String[res.Length, res[0].Length];
                    for (int i = 0; i != res.Length; i++)
                        for (int j = 0; j != res[0].Length; j++)
                            twoD[i, j] = res[i][j];
                    txtLoc.Text = fdText.FileName;

                    parsedData = twoD;
                }


But this only populate the second column:
arrDBZ[0,0] would be arrDBZ[null,Goku], arrDBZ[null,Krillin] and so on...

I hope someone can help me with this.

Thanks.
Posted
Comments
[no name] 24-Jul-15 10:33am    
What sort of "help" are you looking for? There is no textbox or arrDBZ in your code anywhere. Copying and pasting code from the internet is not a good way to learn.
antpower943 24-Jul-15 13:16pm    
It's very confusing what you actually want. A 2D array has 2 dimentions, kinda like a map. You need the coordinates (integers) to get your value (string).
arrDBZ[0,0] = "A Value";

Is it this you want?
Richard MacCutchan 24-Jul-15 13:21pm    
Use a collection class (List, Map etc), that can take both values in each entry.

1 solution

You need to use a split function

First, you split your whole text on each NewLine to an array of strings, each string is a line of original text.
Second, on each string of the array, you split on the comma to an array.

You will find a lot of examples on Google
Subject:
- String to Array
- String Split
 
Share this answer
 

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900