locked
How to show the result of intersection RRS feed

  • Question

  • How to show the intersection of two string? Because my code doesn't work.

    Where dr01=dd&ee&ff&gg&hh

    dr02==ff&gg&jj&kk

    dd=apple

    ee=banana

    ff=car

    gg=durian

    hh=orange

    jj=phone

        Private Sub Button1_Click(sender As Object, e As EventArgs) Handles Button1.Click
             Dim commonNamesQuery = dr01.Intersect(dr02)
            Console.WriteLine()
            Console.ReadLine()
            aa = Console.ReadLine()
            Label11.Text = Console.ReadLine()
        End Sub
    • Edited by Mr Hippo Tuesday, April 5, 2016 2:05 PM
    Tuesday, April 5, 2016 1:40 PM

Answers

  • I can't show the result, only blank. And I want to see common element between 2 strings, because I will using the common elements to see another common element with another string which contains few element for making analysis of some thing.

    I believe that doing this efficiently will require a custom class for string manipulation along the lines of the one outlined in this article.  You should be able to add a method to the code in that article which uses an offset from 0 to the length of the longest string in the collection (a value which you can also easily track with a little extra code) to search for words that contain, first the target string and then the target string minus one character until it is empty.  That could take a little bit to execute, depending on the size of the collection and length of the strings, but it should still be an order of magnitude faster than doing it with any other form of normal string manipulation.  The design detailed in that article is an extremely efficient method of partial string comparison; I highly suspect that it represents the foundation of the solution you require.


    Reed Kimble - "When you do things right, people won't be sure you've done anything at all"

    Wednesday, April 6, 2016 6:53 AM
  • How to show the intersection of two string? Because my code doesn't work.

    This is the .net equivalent of your code.  It may not be the best way to do it, considering that the 'strings' are already a collection.

            Dim dd As String = "apple"
            Dim ee As String = "banana"
            Dim ff As String = "car"
            Dim gg As String = "durian"
            Dim hh As String = "orange"
            Dim jj As String = "phone"
    
            Dim dr01 As New List(Of String)
            dr01.Add(dd)
            dr01.Add(ee)
            dr01.Add(ff)
            dr01.Add(gg)
            dr01.Add(hh)
    
            Dim dr02 As New List(Of String)
            dr02.Add(ff)
            dr02.add(gg)
            dr02.add(jj)
    
            Dim Result As String = String.Join(",", dr01.Intersect(dr02))

    Wednesday, April 6, 2016 7:57 AM

All replies

  • How to show the intersection of two string

    It's not at all clear what type of application you are using so just to answer the question:

          Dim S1 As String = "abcdefgh"
          Dim S2 As String = "fghijklmn"
          MsgBox(String.Join(Nothing, S1.Intersect(S2)))
    

    Tuesday, April 5, 2016 2:05 PM
  • Intersect would work with Ienumerable objects.

    Have a look in this msdn: https://msdn.microsoft.com/en-us/library/bb460136%28v=vs.100%29.aspx?f=255&MSPPError=-2147217396

    Or are you wanting to see the common element between 2 strings?

    Tuesday, April 5, 2016 2:22 PM
  • How to show the intersection of two string

    It's not at all clear what type of application you are using so just to answer the question:

          Dim S1 As String = "abcdefgh"
          Dim S2 As String = "fghijklmn"
          MsgBox(String.Join(Nothing, S1.Intersect(S2)))

    because my program need to compare many variation of string which control by 2 Combo Box (each combo box is have around 10++ of combination of string). So I did know how to find the intersection

    Tuesday, April 5, 2016 2:37 PM
  • I can't show the result, only blank. And I want to see common element between 2 strings, because I will using the common elements to see another common element with another string which contains few element for making analysis of some thing.
    Tuesday, April 5, 2016 2:40 PM
  • Hi Mr Hippo,

    >>I can't show the result, only blank. And I want to see common element between 2 strings, because I will using the common elements to see another common element with another string which contains few element for making analysis of some thing.

    First of all, could you share your code you have tested and clarify how you want to use the common elements?

    >> How to show the intersection of two string? Because my code doesn't work.

    If you have the issue about getting the common elements between two strings, the Enumerable.Intersect() method is a good choice and the following demo you could have a look.

        Sub Main()
            Dim dr01 As String = "dd&ee&ff&gg&hh"
            Dim dr02 As String = "ff&gg&jj&kk"
            Dim array1 As String() = dr01.Split("&")
            Dim array2 As String() = dr02.Split("&")
            Dim intersection As IEnumerable(Of String) = array1.Intersect(array2)
            For Each item As String In intersection
                Console.WriteLine(item)
            Next
            Console.Read()
        End Sub
    

    Best Regards,

    Albert Zhang

    Wednesday, April 6, 2016 6:32 AM
  • I can't show the result, only blank. And I want to see common element between 2 strings, because I will using the common elements to see another common element with another string which contains few element for making analysis of some thing.

    I believe that doing this efficiently will require a custom class for string manipulation along the lines of the one outlined in this article.  You should be able to add a method to the code in that article which uses an offset from 0 to the length of the longest string in the collection (a value which you can also easily track with a little extra code) to search for words that contain, first the target string and then the target string minus one character until it is empty.  That could take a little bit to execute, depending on the size of the collection and length of the strings, but it should still be an order of magnitude faster than doing it with any other form of normal string manipulation.  The design detailed in that article is an extremely efficient method of partial string comparison; I highly suspect that it represents the foundation of the solution you require.


    Reed Kimble - "When you do things right, people won't be sure you've done anything at all"

    Wednesday, April 6, 2016 6:53 AM
  • How to show the intersection of two string? Because my code doesn't work.

    This is the .net equivalent of your code.  It may not be the best way to do it, considering that the 'strings' are already a collection.

            Dim dd As String = "apple"
            Dim ee As String = "banana"
            Dim ff As String = "car"
            Dim gg As String = "durian"
            Dim hh As String = "orange"
            Dim jj As String = "phone"
    
            Dim dr01 As New List(Of String)
            dr01.Add(dd)
            dr01.Add(ee)
            dr01.Add(ff)
            dr01.Add(gg)
            dr01.Add(hh)
    
            Dim dr02 As New List(Of String)
            dr02.Add(ff)
            dr02.add(gg)
            dr02.add(jj)
    
            Dim Result As String = String.Join(",", dr01.Intersect(dr02))

    Wednesday, April 6, 2016 7:57 AM