Follow

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use
Contact

Get text from each line in RichTextBox?

Logo C sharp Logo C sharp
Logo C sharp

I want to take a text from each line in the RichTextBox and add a constant text to the beginning and end of this text line. And when I press the button, the new text will be exported/saved to a text file. I was able to create a loop, but how can I add a condition to the beginning and the end?

private void button1_Click(object sender, EventArgs e)
{   
    string text1 = "hello";
    string text2 = richboxtext.Text;
    string text3 = "goodbye";

    for (int i = 0; i < text2.Length; i++)
    {
        if(...)
        {
           ...
        }
    }
    File.WriteAllText(@"C:\Temp\exapmle.txt", text);
}

>Solution :

string textb = "hello";            
string texte = "goodbye";
var sb = new StringBuilder();
foreach(var line in richTextBox1.Lines)
{
    sb.AppendLine(textb + line + texte);
}
File.WriteAllText(@"C:\Temp\exapmle.txt", sb.ToString());
Add a comment

Leave a Reply

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use

Discover more from Dev solutions

Subscribe now to keep reading and get access to the full archive.

Continue reading