Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Table of strings in C#

Tags:

c#

Is there a way of creating a table with each cell containing a string in C# ?

The closest thing I found is multidimensional arrays string[,] names;, but it seems like its length needs to be defined which is a problem to me.

Here is what my code looks like :

string[] namePost;
int[] numbPage;
string post="";
string newPost;
int i=0;
int j=0;

foreach (var line in File.ReadLines(path).Where(line => regex1.Match(line).Success))
            {
                newPost = regex1.Match(line).Groups[1].Value;
                if (String.Compare(newPost, post) == 0)
                {
                    j = j + 1;
                }
                else
                {
                    namePost[i] = post;
                    numbPage[i] = j;
                    post = newPost;
                    j = 1;
                    i = i + 1;
                }    
            }

Each instance of the for writes the name of the new "post" in a cell of namePost. In the end, the namePost table stores the name of all the posts that are different from one another.

What is the best way to achieve that ?

like image 391
Loukoum Mira Avatar asked Oct 30 '25 10:10

Loukoum Mira


1 Answers

If you are simply trying to store the posts, you can use the List class from the System.Collections.Generic namespace:

using System.Collections.Generic;
List<String> namePost = new List<String>();

Then, instead of namePost[i] = post;, use

namePost.Add(post);
like image 177
MorganG Avatar answered Nov 02 '25 00:11

MorganG