简体   繁体   中英

delete specific lines from richTextBox?

I have a richTextBox1 and has more than 50 lines(some of the lines are empty)... I like to remove the lines only start with ALTER TABLE and contains MOVE STORAGE until the next empty line... For example below first line of words (in the richtextbox has actually two lines and after that empty line comes) start with ALTER TABLE and has MOVE STORAGE so I need to delete everything until next empty line.

ALTER TABLE "CAMPUS_SITE" MOVE STORAGE ( INITIAL 65536 NEXT 1048576 MINEXTENTS 1 MAXEXTENTS 2147483645 PCTINCREASE 0 FREELISTS 1 FREELIST GROUPS 1 BUFFER_POOL DEFAULT FLASH_CACHE DEFAULT CELL_FLASH_CACHE DEFAULT);

my code:

        var text = "";//Holds the text of current line being looped.
        var startindex = 0;//The position where selection starts.
        var endindex = 0;//The length of selection.

        for (int i = 0; i < richTextBox1.Lines.Length; i++)//Loops through each line of text in RichTextBox
        {
            text = richTextBox1.Lines[i];//Stores current line of text.
            if (text.Contains("MOVE STORAGE") == true)//Checks if the line contains MOVE STORAGE.
            {
                startindex = richTextBox1.GetFirstCharIndexFromLine(i);//If match is found the index of first char of that line is stored in startindex.
                endindex = text.Length;//Gets the length of line till semicolon and stores it in endindex.
                richTextBox1.Select(startindex, endindex);//Selects the text.
                richTextBox2.Text = richTextBox2.Text.Replace(richTextBox1.SelectedText, string.Empty);//Replaces the text with empty string.
            }
        }

For removing all the lines that start with alter table and contain move storage, you can do this

List<string> finalLines = richTextBox1.Lines.ToList();
finalLines.RemoveAll(x => x.StartsWith("ALTER TABLE") && x.Contains("MOVE STORAGE"));
richTextBox1.Lines = finalLines.ToArray();
String[] txt = richTextBox1.Lines;
bool flag = false;
for (int i = 0; i < txt.Length; i++)
{
    if (txt[i].StartsWith("ALTER TABLE") && txt[i].Contains("MOVE STORAGE"))
       flag = true;
    if (string.IsNullOrEmpty(txt[i]))
       flag = false;

     if (flag)
       txt[i] = string.Empty;
 }
 richTextBox1.Lines = txt;

This code not delete your lines, but set String.empty (equals "") to this line.

if you need remove all text from your line to empty line, you can use

String[] txt = richTextBox1.Lines;
            richTextBox1.Text = string.Empty;
            bool flag = false;
            for (int i = 0; i < txt.Length; i++)
            {
                if (txt[i].StartsWith("ALTER TABLE") && txt[i].Contains("MOVE STORAGE"))
                    flag = true;
                if (string.IsNullOrEmpty(txt[i]))
                    flag = false;

                if (!flag)
                    richTextBox1.Text += txt[i] + "\r\n";
            }

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM