简体   繁体   English

如何删除不包含特定文本的 vb.net Richtextbox 行?

[英]How to remove vb.net Richtextbox lines that not contains specific text?

I use the next code to remove lines from Richtextboxes but that way i can only tell what line to remove.我使用下一个代码从 Richtextboxes 中删除行,但这样我只能告诉要删除的行。 I need to remove all lines that not contains specific text, can this be done with some edits of my code?我需要删除所有不包含特定文本的行,这可以通过对我的代码进行一些编辑来完成吗?

1st piece:第一段:

Private Property lineToBeRemovedlineToBeRemoved As Integer

2nd piece:第二件:

Dim lineToBeRemoved As Integer = 0
        lineToBeRemovedlineToBeRemoved = lineToBeRemoved - 0
        Dim str As String = RichTextBox1.Lines(lineToBeRemoved)
        RichTextBox1.Find(str & vbCr)
        RichTextBox1.SelectedText = ""

You code is not close.你的代码不接近。 You should start over.你应该重新开始。 Use a for loop to go through the RichTextBox lines.使用 for 循环遍历 RichTextBox 行。 If the text is not in a line, then delete it.如果文本不在一行中,则将其删除。 Tip: It may be easier to go from the last line to the first to avoid problems when deleting.提示:从最后一行转到第一行可能更容易,以避免删除时出现问题。

This code will remove any line from a richtextbox RichTextbox1 that does not contain "Test" on it.此代码将从RichTextbox1中删除不包含“Test”的任何行。 Remember to add Imports System.Text.RegularExpressions to the top of your code.请记住将Imports System.Text.RegularExpressions添加到代码的顶部。

Private Sub RemoveLines()
    Dim lines As New List(Of String)
    lines = RichTextBox1.Lines.ToList
    Dim FilterText = "Test"

    For i As Integer = lines.Count - 1 To 0 Step -1
        If Not Regex.IsMatch(lines(i), FilterText) Then
            lines.RemoveAt(i)
        End If
    Next

    RichTextBox1.Lines = lines.ToArray

End Sub
 Private Sub Button1_Click(sender As Object, e As EventArgs) Handles Button1.Click
    RTB.Select(RTB.GetFirstCharIndexFromLine(2), RTB.Lines(2).Count)
    RTB.SelectionLength = RTB.Lines(2).Length + 1
    RTB.SelectedText = ""
End Sub

Try this code, I applied to my prog and it work good.试试这个代码,我申请了我的编,它工作得很好。 When use, just ... call dels("unwanted") ==> Line which contain unwanted word will disappear.使用时,只需 ... call dels("unwanted") ==> 包含不需要的单词的行将消失。

    Private Sub dels(sw As String)
    Dim ud As String = ""  'for keep all we need
    Dim cn As Integer = 0 'for avoid empty line 
    For Each line As String In RichTextBox1.Lines 'for every line in reichtextbox
        If Len(line) > 5 Then 'if that line got more than 5 character
            If InStr(line.ToLower, sw.ToLower) < 1 Then 'transform them to lower case for better resulted
                If cn = 1 Then ud = ud + vbCrLf 'not place new-line if it is first 
                ud = ud + line 'keep this line if not match ne want delete
                cn = 1 'turn-off first line signal
            End If
        End If
    Next
    RichTextBox1.Clear() 'empty richtextbox
    RichTextBox1.AppendText(ud) 'update richtextbox with the unwanted 
    End Sub

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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