Results 1 to 8 of 8

Thread: [2008] Check if value is in string array

  1. #1

    Thread Starter
    Member
    Join Date
    Nov 2007
    Posts
    62

    [2008] Check if value is in string array

    How would I check if a value is in a string array?

    Like say there is pages(10) and pages(5) is "rhijaen"

    I want it to do something like:

    If "rhijaen" isin pages() then
    dothis
    end if

  2. #2
    Frenzied Member MaximilianMayrhofer's Avatar
    Join Date
    Aug 2007
    Location
    IM IN YR LOOP
    Posts
    2,001

    Re: [2008] Check if value is in string array

    Code:
    If pages.Contains("rhijaen") Then
        'Do something
    End If

  3. #3

    Thread Starter
    Member
    Join Date
    Nov 2007
    Posts
    62

    Re: [2008] Check if value is in string array

    Quote Originally Posted by MaximilianMayrhofer
    Code:
    If pages.Contains("rhijaen") Then
        'Do something
    End If
    This gives me the error: 'contains' is not a member of 'System.Array'.

  4. #4

  5. #5
    PowerPoster SJWhiteley's Avatar
    Join Date
    Feb 2009
    Location
    South of the Mason-Dixon Line
    Posts
    2,256

    Re: [2008] Check if value is in string array

    You probably want to think about using a generic collection, for example a List(of String), rather than an array. It also offers you greater manipulative methods.

    If the worst comes to the worst, you can simply iterate through the collection and compare the strings.

    (Another alternative may be a LINQ query).

  6. #6
    Frenzied Member MaximilianMayrhofer's Avatar
    Join Date
    Aug 2007
    Location
    IM IN YR LOOP
    Posts
    2,001

    Re: [2008] Check if value is in string array

    Works just fine for me!

    Code:
    Dim pages() As String = New String() {"Hello"}
    
    If pages.Contains("Hello") Then _
        Console.WriteLine("It worked!")
    
    Console.ReadLine()

  7. #7
    New Member
    Join Date
    Aug 2009
    Posts
    1

    Re: [2008] Check if value is in string array

    That Contains method wasn't implemented until 3.5.

    Check out this link for details on how to make it work, pre-3.5.

    http://www.dotnettoad.com/index.php?....Contains.html

    cope snippet...

    CType(strYourStringArray, IList).Contains(strYourCheckValue)
    Last edited by TSMontana; Aug 24th, 2009 at 11:27 PM.

  8. #8
    Master Of Orion ForumAccount's Avatar
    Join Date
    Jan 2009
    Location
    Canada
    Posts
    2,802

    Re: [2008] Check if value is in string array

    Although this is old, you can use the Array.IndexOf method to do the same as contains. If it returns greater then -1 then that means true.

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  



Click Here to Expand Forum to Full Width