AndrewH
AndrewH

Reputation: 69

C# - store strings in List

only working on C# for 2 weeks. I've been searching on this problem for a few days and cannot find a simple answer, or answers don't do what i want. So here goes.

I am reading from a text file line by line (no problems).

Example:

A Line MSH
A Line 2
B Line MSH
B Line 2
B Line 3

I want to store the lines in a list like the following ;

List lStore[0,0] = A Line MSH
     lStore[0,1] = A Line 2
     lStore[1,0] = B Line MSH
     lStore[1,1] = B Line 2
     lStore[1,2] = B Line 3

I've been using :

 List<string[]> lStore = new List<string[](); 

to define the list.

I'm looping through the File Read

while ((sFileContents = srFile.ReadLine()) != null)
{
    Boolean bMSH_Start = sFileContents.Contains("MSH");
    if (bMSH_Start)
    {
        // Every time MSH is found i want to start a new record in the list
        lStore.Add(sFileContents);
    }
    else
    {
        ????? //If not MSH then append to current record in list
    }
}

There can be any number of lines in a group, so i am distinguishing by 'MSH' which will appear on the first line of every new group.

Upvotes: 0

Views: 4175

Answers (3)

Fung
Fung

Reputation: 3558

Consider to use List<List<string>> instead of List<string[]>, since the size of each record would be different.

List<List<string>> lStore = new List<List<string>>();

//...

while ((sFileContents = srFile.ReadLine()) != null)
{
    if (sFileContents.Contains("MSH") || !lStore.Any())
    {
        // start a new record if MSH is found
        // or this is the first line being read
        lStore.Add(new List<string>());
    }
    // Append current line to last (i.e. current) record
    lStore.Last().Add(sFileContents);
}

Or if you have already come across LINQ:

int i = 0;
List<string[]> lStore = File.ReadAllLines("TextFile1.txt")
    .Select(s => new { Index = s.Contains("MSH") ? ++i : i, Line = s })
    .GroupBy(x => x.Index, (k, g) => g.Select(x => x.Line).ToArray())
    .ToList();

Upvotes: 1

Doan Cuong
Doan Cuong

Reputation: 2614

First of all, to add them one by one, you will need two lists: outerList and innerList. Let start declaring them

List<List<string>> outerList = new List<List<string>>();
List<string> innerList; //inner list will be initialized inside while loop

Next, we'll examine the while loop.

while ((sFileContents = srFile.ReadLine()) != null)
  {
       Boolean bMSH_Start = sFileContents.Contains("MSH");
       if (bMSH_Start)
       { 
        //everytime MSH is found, add inner list to outer list
        //init new inner list, add new item to it
        if(innerList != null)
        {
           outerList.Add(innerList);
        }

        innerList = new List<string>();
        innerList.Add(sFileContents);
       }
       else
       {
          //if not MSH, then add to inner list
          innerList.Add(sFileContents);
       }
  }

Upvotes: 0

Selman Gen&#231;
Selman Gen&#231;

Reputation: 101731

If your lines starts with A,B,C etc.. and always the first line contains MSH you can group them by first letter instead.

var lines = File.ReadLines("filepath")
             .GroupBy(x => x[0])
             .Select(x => x.ToArray())
             .ToList();

This will give you a List<string[]> as a result.Here is the result in LINQPad:

enter image description here

Upvotes: 2

Related Questions