none
Find string for every line RRS feed

  • Question

  • This question same with previous thread but this one try to to find string for each line not for all line;

    example:Line 1

    StringToFind = 0 ,2 ,
    StringToCheck = 51 ,52 ,56 ,12 ,16 ,26 ,49 ,47 ,47 ,97 ,97 ,77 ,68 ,61 ,61 ,81 ,81 ,11 ,16 ,15 ,12 ,65 ,62 ,52 ,68 ,62 ,63 ,82 ,83 ,23 ,34 ,37 ,36 ,47 ,46 ,76 ,26 ,21 ,23 ,61 ,63 ,13 ,32 ,30 ,36 ,20 ,26 ,06 ,10 ,14 ,10 ,04 ,00 ,40 ,11 ,16 ,13 ,16 ,13 ,63 ,46 ,46 ,44 ,66 ,64 ,64 ,92 ,93 ,90 ,23 ,20 ,30 ,35 ,32 ,31 ,52 ,51 ,21 ,15 ,19 ,12 ,59 ,52 ,92 ,29 ,24 ,24 ,94 ,94 ,44 ,26 ,25 ,26 ,65 ,66 ,56 ,44 ,45 ,46 ,45 ,46 ,56 ,36 ,35 ,34 ,65 ,64 ,54 ,20 ,21 ,25 ,01 ,05 ,15 ,94 ,95 ,96 ,45 ,46 ,56 ,57 ,50 ,51 ,70 ,71 ,01 ,43 ,49 ,41 ,39 ,31 ,91 ,96 ,93 ,92 ,63 ,62 ,32 ,

    Result show = Occurences (02)= nothing,Occurences (20)= 3 ,

    example:Line 2

    StringToFind = 3 ,8 ,
    StringToCheck = 72 ,72 ,72 ,22 ,22 ,22 ,75 ,74 ,77 ,54 ,57 ,47 ,55 ,58 ,56 ,58 ,56 ,86 ,39 ,38 ,37 ,98 ,97 ,87 ,61 ,65 ,66 ,15 ,16 ,56 ,46 ,41 ,44 ,61 ,64 ,14 ,76 ,74 ,77 ,64 ,67 ,47 ,33 ,31 ,38 ,31 ,38 ,18 ,37 ,39 ,33 ,79 ,73 ,93 ,27 ,23 ,23 ,73 ,73 ,33 ,12 ,18 ,19 ,28 ,29 ,89 ,36 ,33 ,36 ,63 ,66 ,36 ,35 ,32 ,35 ,52 ,55 ,25 ,53 ,52 ,53 ,32 ,33 ,23 ,27 ,23 ,29 ,73 ,79 ,39 ,77 ,75 ,70 ,75 ,70 ,50 ,07 ,00 ,08 ,70 ,78 ,08 ,69 ,60 ,69 ,90 ,99 ,09 ,22 ,22 ,21 ,22 ,21 ,21 ,65 ,68 ,60 ,58 ,50 ,80 ,72 ,75 ,78 ,25 ,28 ,58 ,84 ,85 ,83 ,45 ,43 ,53 ,78 ,73 ,75 ,83 ,85 ,35 ,

    Result show = Occurences (38)= 3 ,Occurences (83)= 2 ,

    Option Strict On
    Option Explicit On
    Public Class Form1
    
        Private Data1 As New List(Of String)
        Private Data2 As New List(Of String)
    
    
        Private Items As New List(Of Txt)
        Private Items_A As New List(Of Txt)
        Private Sub Form1_Load(sender As Object, e As EventArgs) Handles MyBase.Load
    
            'Data1.AddRange(IO.File.ReadAllLines(IO.Path.Combine(My.Computer.FileSystem.SpecialDirectories.MyDocuments, "StringToFind.txt")))
            'Data2.AddRange(IO.File.ReadAllLines(IO.Path.Combine(My.Computer.FileSystem.SpecialDirectories.MyDocuments, "StringToCheck.txt")))
    
            'file text =StringToFind.txt
            '0 ,2 ,
            '3 ,8 ,
            '4 ,6 ,
    
            'file text =StringToCheck.txt
            '50 ,52 ,53 ,02 ,03 ,23 ,98 ,98 ,92 ,88 ,82 ,82 ,79 ,79 ,75 ,99 ,95 ,95 ,27 ,28 ,28 ,78 ,78 ,88 ,39 ,36 ,34 ,96 ,94 ,64 ,86 ,84 ,89 ,64 ,69 ,49 ,06 ,07 ,04 ,67 ,64 ,74 ,36 ,37 ,35 ,67 ,65 ,75 ,87 ,83 ,80 ,73 ,70 ,30 ,55 ,51 ,55 ,51 ,55 ,15 ,82 ,89 ,82 ,29 ,22 ,92 ,46 ,45 ,41 ,65 ,61 ,51 ,49 ,42 ,40 ,92 ,90 ,20 ,72 ,75 ,76 ,25 ,26 ,56 ,14 ,12 ,17 ,42 ,47 ,27 ,98 ,98 ,96 ,88 ,86 ,86 ,06 ,03 ,00 ,63 ,60 ,30 ,04 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76 ,75 ,65 ,56 ,53 ,58 ,63 ,68 ,38 ,67 ,60 ,62 ,70 ,72 ,02 ,85 ,80 ,83 ,50 ,53 ,03 ,11 ,12 ,18 ,12 ,18 ,28 ,27 ,26 ,22 ,76 ,72 ,62 ,89 ,85 ,84 ,95 ,94 ,54 ,20 ,21 ,26 ,01 ,06 ,16 ,83 ,81 ,87 ,31 ,37 ,17 ,43 ,47 ,48 ,37 ,38 ,78 ,
    
            For i As Integer = 0 To Data1.Count - 1
                TextBox1.AppendText(Data1(i).ToString & vbCrLf)
                Items.Add(New Txt(Data1(i)))
            Next
    
            For i As Integer = 0 To Data2.Count - 1
                TextBox2.AppendText(Data2(i).ToString & vbCrLf)
                Items_A.Add(New Txt(Data2(i)))
            Next
    
        End Sub
    
        Private Sub Button1_Click(sender As Object, e As EventArgs) Handles Button1.Click
    
            Dim sb As New System.Text.StringBuilder
    
            Dim sourceList As New List(Of String)
            For Each t1 In Items_A
                sourceList.AddRange(From t2 In t1.str_.Split(","c) Where Not String.IsNullOrEmpty(t2) Select t2.Trim)
            Next
    
            Dim counts As New Dictionary(Of String, Integer)
            For Each t1 In Items
                Dim t3 = (From t2 In t1.str_.Split(","c) Where Not String.IsNullOrEmpty(t2) Select t2.Trim).ToArray
                Dim key = Join(t3, "")
                If Not counts.ContainsKey(key) Then counts.Add(key, (From t4 In sourceList Where t4 = key).Count)
                key = Join((t3.Reverse).ToArray, "")
                If Not counts.ContainsKey(key) Then counts.Add(key, (From t4 In sourceList Where t4 = key).Count)
            Next
    
            For Each key As String In (From k In counts.Keys Order By k)
                If counts(key) > 0 Then
                    sb.Append("Occurences (" & key & ")= " & counts(key).ToString & " ," & vbCrLf)
                Else
                    sb.Append("Occurences (" & key & ")= nothing")
                End If
    
            Next
    
            TextBox3.AppendText(sb.ToString)
    
        End Sub
    End Class
    Public Class Txt
        Public Property str_ As String
        Public Sub New(ByVal a As String)
            str_ = a
        End Sub
        Public Overrides Function ToString() As String
            Return Str_
        End Function
    End Class
    

    Thank.

    Saturday, November 16, 2019 1:43 PM

Answers

  • I can Illustrate like bellow;

    Hi

    Try this stand alone example.Needs a blank DataGridView1 on Form1.

    Ther data is in a file, each line has one block of data.

    ' Form1 with blank DataGridView1
    Option Strict On
    Option Explicit On
    Public Class Form1
    	Dim path As String = "C:\Users\lesha\Desktop\Test1.txt"
    	Dim Data_1 As New List(Of String)
    	Private Sub Form1_Load(sender As Object, e As EventArgs) Handles MyBase.Load
    		With DataGridView1
    			.Columns.Add("Column1", "Data_1")
    			.Columns.Add("Column2", "Data_2")
    			.Columns.Add("Column3", "Result")
    			.RowHeadersVisible = False
    			.Columns("Column1").AutoSizeMode = DataGridViewAutoSizeColumnMode.AllCells
    			.SelectionMode = DataGridViewSelectionMode.FullRowSelect
    			.AllowUserToAddRows = False
    
    			With .Columns("Column2")
    				.AutoSizeMode = DataGridViewAutoSizeColumnMode.Fill
    				.DefaultCellStyle.WrapMode = DataGridViewTriState.True
    				.ReadOnly = True
    			End With
    			With .Columns("Column3")
    				.AutoSizeMode = DataGridViewAutoSizeColumnMode.AllCells
    				.DefaultCellStyle.WrapMode = DataGridViewTriState.True
    				.ReadOnly = True
    			End With
    			For Each line As String In IO.File.ReadAllLines(path)
    				.Rows.Add(Nothing, line)
    			Next
    			.AutoResizeRows()
    		End With
    	End Sub
    	Private Sub DataGridView1_Resize(sender As Object, e As EventArgs) Handles DataGridView1.Resize
    		DataGridView1.AutoResizeRows()
    	End Sub
    	Function GetMat(ind As Integer, p As String) As Integer
    		Dim s As String = DataGridView1("Column2", ind).Value.ToString
    		Return System.Text.RegularExpressions.Regex.Matches(s, p).Count
    	End Function
    	Function GetMatches(ind As Integer) As String
    		Dim ret As String = Nothing
    		Dim p As String = DataGridView1("Column1", ind).Value.ToString
    		ret &= "Occurances (" & p & ") = " & GetMat(ind, p) & vbCrLf
    		ret &= "Occurances (" & RevString(p) & ") = " & GetMat(ind, RevString(p))
    		Return ret
    	End Function
    	Function RevString(s As String) As String
    		Dim pp() As Char = s.ToCharArray
    		Array.Reverse(pp)
    		Return New String(pp)
    	End Function
    	Private Sub DataGridView1_CellValidated(sender As Object, e As DataGridViewCellEventArgs) Handles DataGridView1.CellValidated
    
    		If DataGridView1("Column1", DataGridView1.SelectedRows(0).Index).Value Is Nothing Then Exit Sub
    
    		DataGridView1("Column3", DataGridView1.SelectedRows(0).Index).Value = GetMatches(DataGridView1.SelectedRows(0).Index)
    	End Sub
    End Class


    Regards Les, Livingston, Scotland


    Sunday, November 17, 2019 5:27 PM

All replies

  • Hi

    Here is my approach. Stand alone example code below.

    ' Form1 with:
    ' TextBox1 (for Match String)
    ' TextBox2 (for text block number)
    ' Label1 (for normal Match result)
    ' Label2 (for reverse Match result)
    
    Option Strict On
    Option Explicit On
    Public Class Form1
    	Dim s As New List(Of String)
    	Private Sub Form1_Load(sender As Object, e As EventArgs) Handles MyBase.Load
    		s.Add("51 ,52 ,56 ,12 ,16 ,26 ,49 ,47 ,47 ,97 ,97 ,77 ,68 ,61 ,61 ,81 ,81 ,11 ,16 ,15 ,12 ,65 ,62 ,52 ,68 ,62 ,63 ,82 ,83 ,23 ,34 ,37 ,36 ,47 ,46 ,76 ,26 ,21 ,23 ,61 ,63 ,13 ,32 ,30 ,36 ,20 ,26 ,06 ,10 ,14 ,10 ,04 ,00 ,40 ,11 ,16 ,13 ,16 ,13 ,63 ,46 ,46 ,44 ,66 ,64 ,64 ,92 ,93 ,90 ,23 ,20 ,30 ,35 ,32 ,31 ,52 ,51 ,21 ,15 ,19 ,12 ,59 ,52 ,92 ,29 ,24 ,24 ,94 ,94 ,44 ,26 ,25 ,26 ,65 ,66 ,56 ,44 ,45 ,46 ,45 ,46 ,56 ,36 ,35 ,34 ,65 ,64 ,54 ,20 ,21 ,25 ,01 ,05 ,15 ,94 ,95 ,96 ,45 ,46 ,56 ,57 ,50 ,51 ,70 ,71 ,01 ,43 ,49 ,41 ,39 ,31 ,91 ,96 ,93 ,92 ,63 ,62 ,32")
    		s.Add("72 ,72 ,72 ,22 ,22 ,22 ,75 ,74 ,77 ,54 ,57 ,47 ,55 ,58 ,56 ,58 ,56 ,86 ,39 ,38 ,37 ,98 ,97 ,87 ,61 ,65 ,66 ,15 ,16 ,56 ,46 ,41 ,44 ,61 ,64 ,14 ,76 ,74 ,77 ,64 ,67 ,47 ,33 ,31 ,38 ,31 ,38 ,18 ,37 ,39 ,33 ,79 ,73 ,93 ,27 ,23 ,23 ,73 ,73 ,33 ,12 ,18 ,19 ,28 ,29 ,89 ,36 ,33 ,36 ,63 ,66 ,36 ,35 ,32 ,35 ,52 ,55 ,25 ,53 ,52 ,53 ,32 ,33 ,23 ,27 ,23 ,29 ,73 ,79 ,39 ,77 ,75 ,70 ,75 ,70 ,50 ,07 ,00 ,08 ,70 ,78 ,08 ,69 ,60 ,69 ,90 ,99 ,09 ,22 ,22 ,21 ,22 ,21 ,21 ,65 ,68 ,60 ,58 ,50 ,80 ,72 ,75 ,78 ,25 ,28 ,58 ,84 ,85 ,83 ,45 ,43 ,53 ,78 ,73 ,75 ,83 ,85 ,35")
    	End Sub
    	Private Sub Button1_Click(sender As Object, e As EventArgs) Handles Button1.Click
    		GetMatches()
    	End Sub
    	Function GetMat(i As Integer, p As String) As Integer
    		Return System.Text.RegularExpressions.Regex.Matches(s(i), p).Count
    	End Function
    	Function RevString(s As String) As String
    		Dim pp() As Char = s.ToCharArray
    		Array.Reverse(pp)
    		Return New String(pp)
    	End Function
    	Sub GetMatches()
    		Dim p As String = TextBox1.Text
    		Dim v As Integer = 0
    		Integer.TryParse(TextBox2.Text, v)
    		Label1.Text = "Occurances (" & p & ") = " & GetMat(v, p)
    		Label2.Text = "Occurances (" & RevString(p) & ") = " & GetMat(v, RevString(p))
    	End Sub
    End Class
    



    Regards Les, Livingston, Scotland


    • Edited by leshay Saturday, November 16, 2019 8:13 PM incorporate text blocks
    Saturday, November 16, 2019 3:29 PM
  • Thank you very much Leshay.very good knowledge for me

    If we consider that TextBox2 is List Of string , How another example.?

    Sunday, November 17, 2019 2:34 AM
  • Thank you very much Leshay.very good knowledge for me

    If we consider that TextBox2 is List Of string , How another example.?

    Hi

    You will need to provide an example of what you want.


    Regards Les, Livingston, Scotland

    Sunday, November 17, 2019 12:17 PM
  • I can Illustrate like bellow;

    Sunday, November 17, 2019 3:35 PM
  • Hi

    So you already have code that does that - what else is needed?


    Regards Les, Livingston, Scotland

    Sunday, November 17, 2019 3:39 PM
  • No Leshay I don't have it.

    Just figure one by one

    Dim Term As String = "02"

    Dim Count as Integer = 0 For Each Line As String In data_2 If Line.Contains(Term) = True Then

    count += 1

    TextBox3.AppendText("Occurance " & Term & " =" & Count & VBCrf)

    Exit For End If Next

    Sunday, November 17, 2019 3:53 PM
  • I can Illustrate like bellow;

    Hi

    Try this stand alone example.Needs a blank DataGridView1 on Form1.

    Ther data is in a file, each line has one block of data.

    ' Form1 with blank DataGridView1
    Option Strict On
    Option Explicit On
    Public Class Form1
    	Dim path As String = "C:\Users\lesha\Desktop\Test1.txt"
    	Dim Data_1 As New List(Of String)
    	Private Sub Form1_Load(sender As Object, e As EventArgs) Handles MyBase.Load
    		With DataGridView1
    			.Columns.Add("Column1", "Data_1")
    			.Columns.Add("Column2", "Data_2")
    			.Columns.Add("Column3", "Result")
    			.RowHeadersVisible = False
    			.Columns("Column1").AutoSizeMode = DataGridViewAutoSizeColumnMode.AllCells
    			.SelectionMode = DataGridViewSelectionMode.FullRowSelect
    			.AllowUserToAddRows = False
    
    			With .Columns("Column2")
    				.AutoSizeMode = DataGridViewAutoSizeColumnMode.Fill
    				.DefaultCellStyle.WrapMode = DataGridViewTriState.True
    				.ReadOnly = True
    			End With
    			With .Columns("Column3")
    				.AutoSizeMode = DataGridViewAutoSizeColumnMode.AllCells
    				.DefaultCellStyle.WrapMode = DataGridViewTriState.True
    				.ReadOnly = True
    			End With
    			For Each line As String In IO.File.ReadAllLines(path)
    				.Rows.Add(Nothing, line)
    			Next
    			.AutoResizeRows()
    		End With
    	End Sub
    	Private Sub DataGridView1_Resize(sender As Object, e As EventArgs) Handles DataGridView1.Resize
    		DataGridView1.AutoResizeRows()
    	End Sub
    	Function GetMat(ind As Integer, p As String) As Integer
    		Dim s As String = DataGridView1("Column2", ind).Value.ToString
    		Return System.Text.RegularExpressions.Regex.Matches(s, p).Count
    	End Function
    	Function GetMatches(ind As Integer) As String
    		Dim ret As String = Nothing
    		Dim p As String = DataGridView1("Column1", ind).Value.ToString
    		ret &= "Occurances (" & p & ") = " & GetMat(ind, p) & vbCrLf
    		ret &= "Occurances (" & RevString(p) & ") = " & GetMat(ind, RevString(p))
    		Return ret
    	End Function
    	Function RevString(s As String) As String
    		Dim pp() As Char = s.ToCharArray
    		Array.Reverse(pp)
    		Return New String(pp)
    	End Function
    	Private Sub DataGridView1_CellValidated(sender As Object, e As DataGridViewCellEventArgs) Handles DataGridView1.CellValidated
    
    		If DataGridView1("Column1", DataGridView1.SelectedRows(0).Index).Value Is Nothing Then Exit Sub
    
    		DataGridView1("Column3", DataGridView1.SelectedRows(0).Index).Value = GetMatches(DataGridView1.SelectedRows(0).Index)
    	End Sub
    End Class


    Regards Les, Livingston, Scotland


    Sunday, November 17, 2019 5:27 PM
  • Hi,

    Do you resolve the issue? I think Leshay provides a good solution. If you resolve the issue, please mark the helpful as answer. It will be beneficial to other community.

    Best Regards,

    Julie


    MSDN Community Support Please remember to click "Mark as Answer" the responses that resolved your issue, and to click "Unmark as Answer" if not. This can be beneficial to other community members reading this thread. If you have any compliments or complaints to MSDN Support, feel free to contact MSDNFSF@microsoft.com.

    Tuesday, November 19, 2019 7:16 AM
    Moderator
  • Thank you Leshay,Nice and New for me.If we using Datagridview better need to put Column1 with Data_1 automaticaaly.

    Let said Data_1 is

    Dim Data_1 As New List(Of String)

    Data_1.Add("02")

    Data_1.Add("35")

    Data_1.Add("42")

    How to put Data_1 in Column1?

    With DataGridview1

         With Column1 

          ?

         End With

    End With

    Wednesday, November 20, 2019 6:47 AM
  • It settle and working good.

    Thank for remmber me.

    Wednesday, November 20, 2019 6:54 AM
  • It settle and working good.

    Thank for remmber me.

    Hi

    I see you seem to want some further help with this, adding initial values tomColumn1.

    Since you have now closed this thread by marking answer(s), you should open a new thread with a new question.


    Regards Les, Livingston, Scotland

    Wednesday, November 20, 2019 2:15 PM