Concatting Lines into List of strings

I have a text file (that has been loaded into a list for each new line) that I am working with.. its format is something like this:

***CP6***
UNIT, PARTS
    some data here
    some more data here
        more data1
        more data2
    etc111
        etc222
    etc333
    etc444
    etc555
UNIT, PARTS
    11111
    22222
        2.1
        2.2
        2.3
    33333
and so on....

I would like to grab the lines between each UNIT, PARTS and concat it onto a single line to look like this:

theList[0] = UNIT, PARTS\n\tsome data here\n\tsome more data here\n\t\tmore data1\n\t\tmore data2\n\tetc111\n\t\tetc222\n\tetc333\n\tetc444\n\tetc555
theList[1] = UNIT, PARTS\n\t11111\n\t22222\n\t\t2.1\n\t\t2.2\n\t\t2.3\n\t33333
theList[n] = UNIT, PARTS\n\t.......

Can anyone help me with this?

EDIT:

The data is in a List.. So I was thinking something like foreach (var item in fileLineList)...

EDIT2:

I have been messing around and have come up with this.. but it does not seem to work how I intended it to...

                foreach (var line in tempList1)
                {

                    if (isUnitPart == false)
                    {
                        if (line.ToUpper().Contains("\"UNIT\",\"PARTS\""))
                            isUnitPart = true;
                    }
                    else
                    {
                        if (line.ToUpper().Contains("\"UNIT\",\"PARTS\""))
                            isUnitPart = false;
                    }

                    if (isUnitPart == true)
                        concattedUnitPart = concattedUnitPart + line;

                    else
                    {
                        theList.Add(concattedUnitPart + Environment.NewLine);
                    }
                }

Answers


var units = new Regex("UNIT, PARTS",RegexOptions.Multiline)
          .Split(myString)
           .Where(c=> !string.IsNullOrEmpty(c)).ToArray();

myString.Split(new string[]{"UNIT, PARTS"}, StringSplitOptions.None) will give you

theList[0] = \n\tsome data here\n\tsome more data here\n\t\tmore data1\n\t\tmore data2\n\tetc111\n\t\tetc222\n\tetc333\n\tetc444\n\tetc555
theList[1] = \n\t11111\n\t22222\n\t\t2.1\n\t\t2.2\n\t\t2.3\n\t33333
theList[n] = \n\t....

which is probably what you want :)


Use ReadAllText to get all lines in a single string.

Edit: If all your data is in a List:

string[] input = IO.File.ReadAllLines(path); //or a List<String>  
string[] delimiter = new[] { "UNIT, PARTS" };
string text = String.Join(Environment.NewLine, input);
var lines = from word in text.Split(delimiter, StringSplitOptions.None)      
            select line = (delimiter[0] + word)

You need to prefix the result words with the delimiter itself because String.Split removes the delimiter from the returned array.

http://msdn.microsoft.com/en-us/library/c1bs0eda.aspx


Need Your Help

Stackoverflow and function pointers

c++ stack-overflow function-pointers

I'm quite lost on this one and I hope someone here could help.

Lambda to delegate resolution

c# delegates lambda

I'm currently reading Jon Skeet's C# in depth 2nd edition and the following question came to my mind:

About UNIX Resources Network

Original, collect and organize Developers related documents, information and materials, contains jQuery, Html, CSS, MySQL, .NET, ASP.NET, SQL, objective-c, iPhone, Ruby on Rails, C, SQL Server, Ruby, Arrays, Regex, ASP.NET MVC, WPF, XML, Ajax, DataBase, and so on.