locked
How to get an index of an array that partially matches the string? RRS feed

  • Question

  • How to get an index of an array that partially matches the string?

    I have an array of strings DoorFeatures()={"Topjklkjfh", "jhjehfe&6773", etc....}

    I also have an array of Notes()={"Top", "eeeee"}

    I need to check if DoorFeatures contains Notes partially and get an index. So in this case I will be looking for Top and it should give me back DoorFeatures(0) as a result

    Thank you.


    Alex

    Friday, June 17, 2016 3:59 PM

Answers

  • Alex,

    This might get you started but it won't find an index - it will find the value:

    Option Strict On
    Option Explicit On
    Option Infer Off
    
    Public Class Form1
        Private Sub Form1_Load(ByVal sender As System.Object, _
                               ByVal e As System.EventArgs) _
                               Handles MyBase.Load
    
            Dim DoorFeatures() As String = _
                {"Topjklkjfh", "jhjehfe&6773"}
    
            Dim Notes() As String = _
                {"Top", "eeeee"}
    
            For Each s As String In Notes
                Dim text As String = s
    
                Dim qry As System.Collections.Generic.IEnumerable(Of String) = _
                    From df As String In DoorFeatures _
                        Where df.Contains(text)
    
                Stop
            Next
    
    
    
            Stop
    
        End Sub
    End Class


    In the middle of difficulty ... lies opportunity. -- Albert Einstein

    Friday, June 17, 2016 4:06 PM
  • Option Strict On
    
    Public Class Form1
    
        Dim DoorFeatures() As String =
            {"Topjklkjfh", "jhjehfe&6773", "djjTopHYY"}
    
        Dim Notes() As String =
            {"Top", "jeh"}
    
    
        Private Sub Button1_Click(sender As Object, e As EventArgs) Handles Button1.Click
    
            For Each Note As String In Notes
    
                Dim Indexes() As Integer = DoorFeatures.
                    Select(Of Integer)(Function(DoorFeature As String, Index As Integer)
                                           If DoorFeature.Contains(Note) Then
                                               Return Index
                                           Else
                                               Return -1
                                           End If
                                       End Function).
                    Where(Function(i As Integer) i <> -1).
                    ToArray
    
                'Show the indexes
            Next
    
        End Sub
    
    
    End Class


    Luc

    Friday, June 17, 2016 8:11 PM

All replies

  • Alex,

    This might get you started but it won't find an index - it will find the value:

    Option Strict On
    Option Explicit On
    Option Infer Off
    
    Public Class Form1
        Private Sub Form1_Load(ByVal sender As System.Object, _
                               ByVal e As System.EventArgs) _
                               Handles MyBase.Load
    
            Dim DoorFeatures() As String = _
                {"Topjklkjfh", "jhjehfe&6773"}
    
            Dim Notes() As String = _
                {"Top", "eeeee"}
    
            For Each s As String In Notes
                Dim text As String = s
    
                Dim qry As System.Collections.Generic.IEnumerable(Of String) = _
                    From df As String In DoorFeatures _
                        Where df.Contains(text)
    
                Stop
            Next
    
    
    
            Stop
    
        End Sub
    End Class


    In the middle of difficulty ... lies opportunity. -- Albert Einstein

    Friday, June 17, 2016 4:06 PM
  • Option Strict On
    
    Public Class Form1
    
        Dim DoorFeatures() As String =
            {"Topjklkjfh", "jhjehfe&6773", "djjTopHYY"}
    
        Dim Notes() As String =
            {"Top", "jeh"}
    
    
        Private Sub Button1_Click(sender As Object, e As EventArgs) Handles Button1.Click
    
            For Each Note As String In Notes
    
                Dim Indexes() As Integer = DoorFeatures.
                    Select(Of Integer)(Function(DoorFeature As String, Index As Integer)
                                           If DoorFeature.Contains(Note) Then
                                               Return Index
                                           Else
                                               Return -1
                                           End If
                                       End Function).
                    Where(Function(i As Integer) i <> -1).
                    ToArray
    
                'Show the indexes
            Next
    
        End Sub
    
    
    End Class


    Luc

    Friday, June 17, 2016 8:11 PM