簡體   English   中英

如何從richTextBox中刪除/刪除特定文本行?

[英]How can i remove/delete from a richTextBox a specific text line?

我嘗試了這段代碼:

private void RemoveLines(List<string> Keywords)
        {
            string NewText = "";
            String regex = string.Empty;
            this.Invoke(new MethodInvoker(delegate { NewText = richTextBox1.Text; }));
            Regex MyRegex = null;
            foreach (string keyword in Keywords)
            {
                regex = String.Format(@"^.*\W{0}\W.*$",keyword);
                MyRegex = new Regex(regex, RegexOptions.Multiline);
                NewText = MyRegex.Replace(NewText, Environment.NewLine);
            }
            //Remove blank lines
            NewText = Regex.Replace(NewText, @"^\s+$[\r\n]*", "", RegexOptions.Multiline);
            this.Invoke(new MethodInvoker(delegate { richTextBox1.Text = NewText; }));
            this.Invoke(new MethodInvoker(delegate { richTextBox1.Refresh(); }));
        }

和removeExtrenals函數:

private List<string> removeExternals(List<string> externals)
        {
            if(!LocalyKeyWords.ContainsKey(mainUrl))
            {
                return externals;
            }
            List<string> keywords = LocalyKeyWords[mainUrl];
            List<int> indices = new List<int>();
            foreach(string keyword in keywords)
            {
                //Accumulate a list of the indices of the items that match.
                indices = indices.Concat(externals.Select((v, i) => v.Contains(keyword) ? i : -1)).ToList();
            }
            //Filter out the -1s, grab only the unique indices.
            indices = indices.Where(i => i >= 0).Distinct().ToList();
            //Filter out those items that match the keyword(s) related to mainUrl.
            externals = externals.Where((v, i) => !indices.Contains(i)).ToList();
            return externals;
        }

我在此函數中調用removelines和removeexternals:

RemoveLines(removeExternals(webSites));

在使用斷點后的removeExternals中,我看到兩個站點均不包含google(示例中使用的關鍵字im),因此從18個網址中我應該只能在richTextBox中看到16個網址,我在removelines函數中也使用了一個斷點,但沒有例外,但我看到了richTextBox中的所有網址。

嘗試這樣:
我添加了一些隨機數據進行測試..

private void Form1_Load(object sender, EventArgs e)
    {
        richTextBox1.AppendText(@"Loading The Url:   http://www.blogger.com/?tab=wj... Failed ");
        richTextBox1.AppendText(Environment.NewLine);
        richTextBox1.AppendText(@"Loading The Url:   http://www.google.co.il/intl/iw/options/... Done  ");
        richTextBox1.AppendText(Environment.NewLine);
        richTextBox1.AppendText(@"Loading The Url:   http://www.xyz.com/?tab=wj... Failed ");
        richTextBox1.AppendText(Environment.NewLine);
        richTextBox1.AppendText(@"Loading The Url:   http://www.abc.com/?tab=wj... Done ");
        richTextBox1.AppendText(Environment.NewLine);
        richTextBox1.AppendText(@"Loading The Url:   http://www.so.com/?tab=wj... Failed ");
        richTextBox1.AppendText(Environment.NewLine);
        keys.Add("xyz");
        keys.Add("abc");
        RemoveLines(keys);
    }  

這是RemoveLines方法的代碼。

private void RemoveLines(List<string> Keywords)
    {
        String regex = string.Empty;
        string NewText = richTextBox1.Text;            
        Regex MyRegex = null;
        foreach (string keyword in Keywords)
        {
            regex = String.Format(@"^.*\W{0}\W.*$",keyword);
            MyRegex = new Regex(regex, RegexOptions.Multiline);
            NewText = MyRegex.Replace(NewText, Environment.NewLine);
        }
        //Remove blank lines
        NewText = Regex.Replace(NewText, @"^\s+$[\r\n]*", "", RegexOptions.Multiline); 
        richTextBox1.Text = NewText;
        richTextBox1.Refresh();
    }  

希望能幫助到你 !

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM