theNoobGuy
theNoobGuy

Reputation: 1636

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);
                    }
                }

Upvotes: 1

Views: 136

Answers (3)

Tim Schmelter
Tim Schmelter

Reputation: 460238

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

Upvotes: 1

Sleiman Jneidi
Sleiman Jneidi

Reputation: 23339

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

Upvotes: 1

undefined
undefined

Reputation: 34299

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 :)

Upvotes: 1

Related Questions