According to your comment to the solution #1 by
BillWoodruf[
^]...
Before executing the code
First line
Second line
Third line
some text
Fifth line
Sixth line
Seventh line
some different text
ninth line
tenth line
eleventh line
some bla bla
After executing the code
First line
Second line
Third linesome text
Fifth line
Sixth line
Seventh linesome different text
ninth line
tenth line
eleventh linesome bla bla
A Linq solution:
string[] lines = {"First line", "Second line", "Third line",
"some text", "Fifth line", "Sixth line", "Seventh line", "some different text",
"ninth line", "tenth line", "eleventh line", "some bla bla"};
var notnumberedlines = lines
.Where(x=>!x.Contains("line"))
.Select((x,i) => new {Index = ((i+1)*3)-1, Line=x})
.ToList();
var cleanlines = lines.Except(notnumberedlines.Select(x=>x.Line));
var together = from c in cleanlines.Select((x,i)=> new{Index = i, Line = x})
join n in notnumberedlines on c.Index equals n.Index into g
select c.Line + g.Select(y=>y.Line).SingleOrDefault();
For further details, please see:
LINQ: .NET Language-Integrated Query[
^]
Getting Started with LINQ in C# | Microsoft Docs[
^]
join clause (C# Reference) | Microsoft Docs[
^]
[EDIT]
According to our discussion (in the comments to the answer), if you would like to concat every third line with the previous one, try this:
string[] lines = Enumerable.Range(1,50).Select(x=> string.Format("Line{0}",x)).ToArray();
var everythirdline = lines
.Where((x,y)=>(y+1)%3==0)
.Select((x,i) => new {Index = ((i+1)*2)-1, Line=x})
.ToList();
var cleanlines = lines.Except(everythirdline.Select(x=>x.Line));
var together = (from c in cleanlines.Select((x,i)=> new{Index = i, Line = x})
join n in everythirdline on c.Index equals n.Index into g
select c.Line + g.Select(y=>y.Line).SingleOrDefault())
.ToList();
Result:
Line1
Line2Line3
Line4
Line5Line6
Line7
Line8Line9
Line10
Line11Line12
Line13
Line14Line15