【问题标题】:How to remove vb.net Richtextbox lines that not contains specific text?如何删除不包含特定文本的 vb.net Richtextbox 行?
【发布时间】:2020-10-25 15:54:12
【问题描述】:

我使用下一个代码从 Richtextboxes 中删除行,但这样我只能知道要删除的行。我需要删除所有不包含特定文本的行,可以通过对我的代码进行一些编辑来完成吗?

第一件:

Private Property lineToBeRemovedlineToBeRemoved As Integer

第二件:

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

【问题讨论】:

    标签: vb.net visual-studio-2010


    【解决方案1】:

    此代码将删除richtextbox RichTextbox1 中不包含“Test”的任何行。记得在代码顶部添加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
    

    【讨论】:

      【解决方案2】:

      您的代码未关闭。你应该重新开始。使用 for 循环遍历 RichTextBox 行。如果文本不在一行中,则将其删除。提示:从最后一行到第一行可能更容易避免删除时出现问题。

      【讨论】:

        【解决方案3】:
         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
        

        【讨论】:

        • 虽然这段代码 sn-p 可以解决问题,但including an explanation 确实有助于提高帖子的质量。请记住,您是在为将来的读者回答问题,而这些人可能不知道您提出代码建议的原因。
        【解决方案4】:

        试试这个代码,我申请了我的 prog,它运行良好。 使用时,只需...调用 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
        

        【讨论】:

          猜你喜欢
          • 2018-03-15
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          • 2021-11-11
          • 1970-01-01
          • 2013-07-31
          相关资源
          最近更新 更多