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