none
Find certain line in List RRS feed

  • Question

  • Hi All,

    How to find certain value in lines then show the result into textbox3.

    Example :

    search all Line having 1,1,1,2,2,3, then show .

    Option Strict On
    Option Explicit On
    Option Infer Off
    
    Imports System.IO
    Imports System.Data
    
    Public Class Form1
        Dim ETD As New List(Of String)
    
        Private Sub Form1_Load(sender As System.Object, e As System.EventArgs) Handles MyBase.Load
            ETD.Add("1 ,1 ,1 ,1 ,1 ,1 ,2 ,2 ," & vbCrLf)
            ETD.Add("1 ,1 ,1 ,2 ,2 ,3 ," & vbCrLf)
            ETD.Add("1, 1, 1, 1, 1, 1, 2, 2," & vbCrLf)
            ETD.Add("1 ,1 ,1 ,1 ,1 ,2 ,3 ," & vbCrLf)
            ETD.Add("1 ,1 ,1 ,2 ,2 ,3 ," & vbCrLf)
            ETD.Add("1 ,1 ,1 ,2 ,2 ,3 ," & vbCrLf)
            ETD.Add("1 ,1 ,1 ,1 ,1 ,1 ,2 ,2 ," & vbCrLf)
            ETD.Add("1 ,1 ,1 ,2 ,2 ,3 ," & vbCrLf)
            ETD.Add("1 ,1 ,1 ,1 ,1 ,1 ,1 ,1 ,2 ," & vbCrLf)
            ETD.Add("1 ,1 ,1 ,1 ,2 ,2 ,2 ," & vbCrLf)
            ETD.Add("1 ,1 ,1 ,1 ,1 ,2 ,3 ," & vbCrLf)
            ETD.Add("1 ,1 ,1 ,3 ,4 ," & vbCrLf)
            ETD.Add("1 ,1 ,1 ,2 ,2 ,3 ," & vbCrLf)
            ETD.Add("1 ,1 ,1 ,2 ,2 ,3 ," & vbCrLf)
            ETD.Add("1 ,1 ,1 ,2 ,2 ,3 ," & vbCrLf)
            ETD.Add("1 ,2 ,2 ,2 ,3 ," & vbCrLf)
            ETD.Add("1 ,1 ,1 ,2 ,2 ,3 ," & vbCrLf)
            ETD.Add("1 ,1 ,1 ,1 ,1 ,1 ,1 ,3 ," & vbCrLf)
            ETD.Add("1 ,1 ,1 ,1 ,2 ,2 ,2 ," & vbCrLf)
            ETD.Add("1 ,1 ,1 ,1 ,1 ,1 ,2 ,2 ," & vbCrLf)
            ETD.Add("1 ,1 ,1 ,1 ,1 ,2 ,3 ," & vbCrLf)
            ETD.Add("1 ,1 ,1 ,2 ,2 ,3 ," & vbCrLf)
            ETD.Add("1 ,1 ,1 ,1 ,2 ,4 ," & vbCrLf)
            ETD.Add("1 ,1 ,1 ,2 ,2 ,3 ," & vbCrLf)
            ETD.Add("1 ,1 ,1 ,1 ,2 ,2 ,2 ," & vbCrLf)
            ETD.Add("1 ,1 ,1 ,1 ,2 ,2 ,2 ," & vbCrLf)
            ETD.Add("1 ,1 ,1 ,1 ,1 ,2 ,3 ," & vbCrLf)
            ETD.Add("1 ,1 ,1 ,1 ,1 ,2 ,3 ," & vbCrLf)
            ETD.Add("1 ,1 ,1 ,1 ,1 ,1 ,1 ,3 ," & vbCrLf)
            ETD.Add("1 ,1 ,2 ,2 ,2 ,2 ," & vbCrLf)
            ETD.Add("1 ,1 ,1 ,1 ,2 ,2 ,2 ," & vbCrLf)
            ETD.Add("1 ,1 ,1 ,2 ,2 ,3 ," & vbCrLf)
            ETD.Add("1 ,1 ,1 ,1 ,3 ,3 ," & vbCrLf)
            ETD.Add("1 ,1 ,1 ,1 ,1 ,2 ,3 ," & vbCrLf)
            ETD.Add("1 ,1 ,1 ,1 ,1 ,2 ,3 ," & vbCrLf)
            ETD.Add("1 ,1 ,1 ,2 ,2 ,3 ," & vbCrLf)
            ETD.Add("1 ,1 ,1 ,1 ,1 ,1 ,2 ,2 ," & vbCrLf)
            ETD.Add("1 ,1 ,1 ,1 ,2 ,2 ,2 ,")
    
        End Sub
    
        Private Sub Button1_Click(sender As System.Object, e As System.EventArgs) Handles Button1.Click
            For I As Integer = 0 To ETD.Count - 1
                TextBox1.AppendText(ETD(I))
            Next
    
        End Sub
    
        Private Sub Button2_Click(sender As System.Object, e As System.EventArgs) Handles Button2.Click
    
            For I As Integer = 0 To ETD.Count - 2
                TextBox2.AppendText("Comparing Line " & I.ToString & " : " & ETD(I).ToString)
                For J As Integer = I + 1 To ETD.Count - 1
                    TextBox2.AppendText("To           " & J.ToString & " : " & ETD(J).ToString)
                    If ETD(I).ToString = ETD(J).ToString Then
                        TextBox3.AppendText(ETD(I).ToString)
                    End If
                Next
            Next
    
        End Sub
    End Class
    

    thank

    Sunday, September 17, 2017 11:27 AM

Answers

  •  Well,  here are two different ways....  You may want to use the Contains method instead of the StartsWith method if you want to find it located anywhere within each line.

        Private Sub Button1_Click(sender As Object, e As EventArgs) Handles Button1.Click
            Dim SearchString As String = "1 ,1 ,1 ,2 ,2 ,3 ,"
            Dim lines() As String = ETD.Where(Function(x) x.StartsWith(SearchString)).ToArray
            TextBox3.Lines = lines
        End Sub
    
        Private Sub Button2_Click(sender As Object, e As EventArgs) Handles Button2.Click
            Dim SearchString As String = "1 ,1 ,1 ,2 ,2 ,3 ,"
            Dim lines() As String = (From s As String In ETD Where s.StartsWith(SearchString)).ToArray
            TextBox3.Lines = lines
        End Sub
     

     

     EDIT:  And here is a third way using a For Each loop...  I recommend appending the lines to a StringBuilder and then after the loop exits and all the lines are appended,  assign it to the TextBox's Text property.  It would be much quicker than appending it right to the TextBox.

        Private Sub Button3_Click(sender As Object, e As EventArgs) Handles Button3.Click
            Dim SearchString As String = "1 ,1 ,1 ,2 ,2 ,3 ,"
            Dim sb As New System.Text.StringBuilder
            For Each s As String In ETD
                If s.StartsWith(SearchString) Then
                    sb.AppendLine(s)
                End If
            Next
            TextBox3.Text = sb.ToString
        End Sub
    




    If you say it can`t be done then i`ll try it

    • Edited by IronRazerz Sunday, September 17, 2017 12:13 PM
    • Marked as answer by mipakteh Sunday, September 17, 2017 12:37 PM
    Sunday, September 17, 2017 11:44 AM