0

我有一个 richTextBox1 并且有超过 50 行(有些行是空的)...我喜欢删除仅以 ALTER TABLE 开头的行并包含 MOVE STORAGE 直到下一个空行...例如下面的第一行单词(在richtextbox中实际上有两行,然后是空行)以 ALTER TABLE 开头并具有 MOVE STORAGE 所以我需要删除所有内容,直到下一个空行。

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

我的代码:

        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.
            }
        }
4

2 回答 2

3

要删除以 alter table 开头并包含移动存储的所有行,您可以执行此操作

List<string> finalLines = richTextBox1.Lines.ToList();
finalLines.RemoveAll(x => x.StartsWith("ALTER TABLE") && x.Contains("MOVE STORAGE"));
richTextBox1.Lines = finalLines.ToArray();
于 2013-08-19T05:42:17.310 回答
1
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;

此代码不会删除您的行,而是将 String.empty (equals "") 设置为该行。

如果您需要将行中的所有文本删除到空行,您可以使用

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";
            }
于 2013-08-19T06:07:02.873 回答