954,535 Members — Technology Publication meets Social Media
Username:
Password:
Lost login information?
Have something to say? Contribute New Article Reply to this Article

Remove Lines Containg Specific Text

Hello Coders
Need help
I want to remove the lines that contain specific text. (Whole Line)

Eg:
Textbox1.Text =

learn
lines dani knows everything
web knows lines
lines knows daniweb everything
dani everything lines


So textbox1.text will become and remove whole line that contains the word "daniweb"

learn
lines dani knows everything
web knows lines
dani everything lines
vb2learn
Newbie Poster
14 posts since Mar 2011
Reputation Points: 10
Solved Threads: 0
 

1. find the position of the word that you are looking for.
2. find the position of the previous and next new line character.
3. delete / remove everything in between.

4. go to step 1

debasisdas
Posting Genius
6,872 posts since Feb 2007
Reputation Points: 666
Solved Threads: 434
 

See if this helps to remove certain lines from a TextBox.

Public Class Form1
    Private arTemp() As String = Nothing '// used to get all lines from TextBox.
    Private sTemp As String = Nothing '// used to add lines back to TextBox.

    Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click
        '//------------- removeLines(text in line to locate, TextBox to search for text)
        TextBox1.Text = removeLines("daniweb", TextBox1)
    End Sub

    Private Function removeLines(ByVal textInLineToFind As String, ByVal selectedTextBox As TextBox) As String
        arTemp = selectedTextBox.Lines '// set all lines from TextBox into a String Array.
        sTemp = "" '// clear for new input.
        For Each txtLine As String In arTemp '// loop thru all arrays.
            If Not txtLine.Contains(textInLineToFind) Then '// check for line that contains preselected text and skip if .Contains.
                '// if not in line, add to string.
                If Not sTemp = "" Then sTemp &= vbNewLine & txtLine Else sTemp = txtLine
            End If
        Next
        Return sTemp '// return text back to TextBox.
    End Function
End Class
codeorder
Posting Virtuoso
1,913 posts since Aug 2010
Reputation Points: 255
Solved Threads: 384
 

Thank You debasisdas

But the example by codeorder worked.

Thank You Codeorder.

Marked As Resolved.

vb2learn
Newbie Poster
14 posts since Mar 2011
Reputation Points: 10
Solved Threads: 0
 

This question has already been solved

Post: Markdown Syntax: Formatting Help
You
View similar articles that have also been tagged: