String table in C #
Is there a way to create a table with each cell that contains a row in C #?
The closest I've found is multidimensional arrays string[,] names;
, but it looks like its length needs to be determined, which is a problem for me.
This 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 for
writes the name of the new "message" in the cell namePost
. At the end, the table namePost
stores the name of all messages that differ from each other.
What is the best way to achieve this?
source to share
If you're just trying to store messages, you can use the List class from the System.Collections.Generic namespace:
using System.Collections.Generic;
List<String> namePost = new List<String>();
Then instead namePost[i] = post;
use
namePost.Add(post);
source to share
DataTable
https://msdn.microsoft.com/en-us/library/system.data.datatable(v=vs.110).aspx
Use this, there is no need to define the length at all.
Helpful guide and examples:
source to share
This will give you all your unique posts. If you want the result to be a list, you can simply do
.ToList ()
with the result.
static IEnumerable<string> AllPosts(Regex regex, string filePath)
{
return File.ReadLines (filePath)
.Where (line => regex.Match (line).Success)
.Select (line => regex.Match (line).Groups [1].Value)
.Distinct ();
}
source to share