locked
Checking if substring of generic.List.element or item contains string RRS feed

  • Question

  • User507956310 posted

    Hello again,

    I am creating a list and add x elements.

     

    Dim ListOfAlternativesSet As New Generic.List(Of String)
    Dim i As Integer = 0
    For Each AddListWithMembershipRow As GridViewRow In GridViewAlt.Rows
    ListOfAlternativesSet.Add(AddListWithMembershipRow.Cells(3).Text + "  _" + AddListWithMembershipRow.Cells(4).Text)
    i = i + 1
    Next
     

    Where + AddListWithMembershipRow.Cells(4).Text refers to an Integer of, lets say 100 (maximum 100 as an integer)
    As you can see I am adding a string element that was created from the two other strings, leaving a "  _" between them.
    So the element in the list becomes: "TotalTemperature  _100"



    In my IF statement, I ask:

     
    If ListOfAlternativesSet.Contains(TotalTemperature) Then xxxx
    Else
    Result=False
    End If

     

    Which results in "False". But it should be true because the List does contain TotalTemperature (the first part of my string
    combination)

    I think I need to re-write the If statement like this:

    (YourString.Length - 3,3)
    If Substring(ListOfAlternativesSet.Contains(AddListWithMembershipRow.Cells(3).Text +"  _").Length -3,3) Then xxxx
    Else
    Result=False
    End If



    In case that possible, what would be the correct syntax? Or is there another way for checking of the generic.list contains parts of "TotalTemperature  _100" ?


    Many thanks,

     

    polynaux


    PS: I can not seperate the strings (need them together at a later stage)
     

    Saturday, April 19, 2008 7:42 AM

Answers

  • User-158764254 posted

    when you check the list with .Contains, it's looking for an exact match on each item - not a partial match.

    to check your list to see if any of its items might be a partial match, you could enumerate the list (for/each) and do a substring check on each item.

    • Marked as answer by Anonymous Thursday, October 7, 2021 12:00 AM
    Saturday, April 19, 2008 5:30 PM

All replies

  • User-158764254 posted

    when you check the list with .Contains, it's looking for an exact match on each item - not a partial match.

    to check your list to see if any of its items might be a partial match, you could enumerate the list (for/each) and do a substring check on each item.

    • Marked as answer by Anonymous Thursday, October 7, 2021 12:00 AM
    Saturday, April 19, 2008 5:30 PM
  • User507956310 posted

     Thanks mbanavige,

     

    that was not the answer I was hoping for :-/

     I am using the list.contains method in about 30 different if statements and it would be a pain to write 30+ loops just to see if there is a partial matching.

     

     I wonder if this would work:

     

    If ListOfAlternatviesSet.FindAll(Temperature EndsWithSaurus) = True Then xx
    Else
    Result=False
    End If




    Private Shared Function EndsWithSaurus(ByVal s As String) _
    As Boolean ' AndAlso prevents evaluation of the second Boolean
    ' expression if the string is so short that an error
    ' would occur.
    If (s.Length > 5) AndAlso _
    (s.Substring(s.Length - 6).ToLower() = "saurus") Then
    Return True
    Else
    Return False
    End If
    End Function
      

     

     

     

    Otherwise It seems I have to think about a new solution to the problem.

     

    Polynaux
     

    Sunday, April 20, 2008 4:56 AM