I like to delete last line of richtextbox which has ended with ; semicolumn. I like to delete this line until ; semicolumn that comes before the last semicolumn.
Example:
hello do not delete this line;
hello this sentence will continue...
untill here;
result should be:
hello do not delete this line;
My Code:
private void button1_Click_1(object sender, EventArgs e) {
List<string> myList = richTextBox1.Lines.ToList();
if (myList.Count > 0) {
myList.RemoveAt(myList.Count - 1);
richTextBox1.Lines = myList.ToArray();
richTextBox1.Refresh();
}
}
Use this:
var last = richTextBox1.Text.LastIndexOf(";");
if (last > 0)
{
richTextBox1.Text = richTextBox1.Text.Substring(0, last - 1);
var beforelast = richTextBox1.Text.LastIndexOf(";");
richTextBox1.Text = richTextBox1.Text.Substring(0, beforelast + 1);
}
else
{
richTextBox1.Text = "";
}
You did not specify the other scenarios(i.e, when the string does not contain ";") this code removes the string starting at ";" just before the last ";" to the last ";". It removes the last semicolon and texts after that, then finds the new last ";". finally removes the text after this ";"