Click here to Skip to main content
15,889,335 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
Hello everyone,
I have a multiline textBox1.Text on the form.
I would like, if some line in textBox1.Text starts with "some", that every such line is move on the end of the lines above that line.

For example:

If my textBox1.Text contains (before executing the code):

First line
Second line
Third line
some text
Fifth line
Sixth line
Seventh line
some text

After executing the code

First line
Second line
Third linesome text
Fifth line
Sixth line
Seventh linesome text

What I have tried:

something like:

if (textbox1.Text.StartsWith("some"))
{
// code here
}

I hope you understand me. Thank you.
Posted
Updated 10-Apr-18 7:05am
Comments
Kornfeld Eliyahu Peter 9-Apr-18 16:09pm    
You may use a regex to replace 'newline-some-text' with 'some-text'...
It should work even for case when you have 'some-text' several times in row...

textBox1.Text = textBox1.Text.Replace("\r\nsome text", "some text");
 
Share this answer
 
Comments
Member 10410972 10-Apr-18 2:54am    
Thank you, Bill, but I thought in fact that every fourth line starts with "some", but the rest of the text of those lines that begin with some is not the same (it differs).
For example:

If my textBox1.Text contains (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
BillWoodruff 10-Apr-18 4:20am    
If you need wild-card search and replace, see Maciej's solution below, and consider using a RegEx.
Maciej Los 10-Apr-18 3:56am    
5ed!
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:
C#
//your lines
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"};
//find lines which does not contain "line" word
var notnumberedlines = lines
	.Where(x=>!x.Contains("line"))
	.Select((x,i) => new {Index = ((i+1)*3)-1, Line=x})
	.ToList();
//remove these lines
var cleanlines = lines.Except(notnumberedlines.Select(x=>x.Line));
//add lines with "some text", "some different text", "somebla bla"
//use left outer join on index
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:

C#
//generate 50 lines: line1, line2, ... , line50
string[] lines = Enumerable.Range(1,50).Select(x=> string.Format("Line{0}",x)).ToArray();

//get every 3. line
var everythirdline = lines
	.Where((x,y)=>(y+1)%3==0)
	.Select((x,i) => new {Index = ((i+1)*2)-1, Line=x})
	.ToList();

//remove these lines
var cleanlines = lines.Except(everythirdline.Select(x=>x.Line));

//use left outer join on index
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 
 
Share this answer
 
v4
Comments
BillWoodruff 10-Apr-18 4:18am    
+5
Maciej Los 10-Apr-18 4:20am    
Thank you, Bill.
Member 10410972 10-Apr-18 7:10am    
Okay, I do not think you understand me well.
To say more simply:
I would like to move every third line in the textBox1.Text on the end of the line that is above that line.

For example:

If my textBox1.Text contains (before executing the code):

First line
Second line
Third line
Fourth line
Fifth line
Sixth line
Seventh line
Eighth line
Ninth line
Tenth line
Eleventh line
Twelfth line

After executing the code

First line
Second lineThird line
Fourth line
Fifth lineSixth line
Seventh line
Eighth lineNinth line
Tenth line
Eleventh lineTwelfth line

Note:
textBox1.Text has about 200 lines.
If it's possible, how can I do it and, if not possible, I apologize.
Thank you.
Maciej Los 10-Apr-18 9:26am    
Assuming that you don't want to remove some line, but to join every 3. line, you have to use another algorithm. See updated answer for next few minutes.
BillWoodruff 10-Apr-18 12:33pm    
Please edit your original post if you have changed the problem.
Quote:
I would like to move every third line in the textBox1.Text on the end of the line that is above that line.
For transforming a collection of lines so that they are formatted with every 3rd. line appended to the second line:
string[] splitLines = textBox1.Text.Split(new char[]
{   
'\r', '\n'
}, StringSplitOptions.RemoveEmptyEntries);

StringBuilder sb = new StringBuilder();

textBox1.Clear();

for (int i = 0; i < splitLines.Length; i++)
{
    sb.Append(splitLines[i]);
    
    if (i == 0 || i % 3 != 2)
    {
        sb.Append("\r\n");
    }
}

textBox1.Text = sb.ToString();
Use a StringBuilder to reduce the unnecessary allocation of memory.
 
Share this answer
 
v2
Comments
Maciej Los 10-Apr-18 13:41pm    
Another 5!

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900