Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • World
  • Users
  • Groups
Skins
  • Light
  • Cerulean
  • Cosmo
  • Flatly
  • Journal
  • Litera
  • Lumen
  • Lux
  • Materia
  • Minty
  • Morph
  • Pulse
  • Sandstone
  • Simplex
  • Sketchy
  • Spacelab
  • United
  • Yeti
  • Zephyr
  • Dark
  • Cyborg
  • Darkly
  • Quartz
  • Slate
  • Solar
  • Superhero
  • Vapor

  • Default (No Skin)
  • No Skin
Collapse
Code Project
  1. Home
  2. General Programming
  3. Visual Basic
  4. How to check array contents

How to check array contents

Scheduled Pinned Locked Moved Visual Basic
data-structurestutorialquestion
6 Posts 4 Posters 0 Views 1 Watching
  • Oldest to Newest
  • Newest to Oldest
  • Most Votes
Reply
  • Reply as topic
Log in to reply
This topic has been deleted. Only users with topic management privileges can see it.
  • K Offline
    K Offline
    KIDYA
    wrote on last edited by
    #1

    Hello Experts!!! I am declaring array arr() as string assign values to array...now i want to search specific content in that array.Can anybody tell me how will i do this?

    D L D 3 Replies Last reply
    0
    • K KIDYA

      Hello Experts!!! I am declaring array arr() as string assign values to array...now i want to search specific content in that array.Can anybody tell me how will i do this?

      D Offline
      D Offline
      Denis J Gonzalez
      wrote on last edited by
      #2

      If you want to search for a String within an array of Strings you can use this function.

      Private Function indexInArray(ByVal theArray As String(), ByVal value As String) As Integer
          For i As Integer = 0 To theArray.Length - 1
              If theArray(i) = value Then Return i
          Next
          Return -1
      End Function
      

      To use this function you have to pass the array you want the string to be searched in and the string you want to search (value). This function will return -1 if it doesn't find the string you want to search within the array. If it does find it it returns the index. NOTE: This only works for one-dimensional arrays. You can use this sub to test it.

      Private Sub testIt()
          Dim myStrings(1) As String
          myStrings(0) = "hello"
          myStrings(1) = "world"
          If indexInArray(myStrings, "hello") <> -1 Then
              MessageBox.Show("The string in contained within the array")
          End If
      End Sub
      
      K 1 Reply Last reply
      0
      • D Denis J Gonzalez

        If you want to search for a String within an array of Strings you can use this function.

        Private Function indexInArray(ByVal theArray As String(), ByVal value As String) As Integer
            For i As Integer = 0 To theArray.Length - 1
                If theArray(i) = value Then Return i
            Next
            Return -1
        End Function
        

        To use this function you have to pass the array you want the string to be searched in and the string you want to search (value). This function will return -1 if it doesn't find the string you want to search within the array. If it does find it it returns the index. NOTE: This only works for one-dimensional arrays. You can use this sub to test it.

        Private Sub testIt()
            Dim myStrings(1) As String
            myStrings(0) = "hello"
            myStrings(1) = "world"
            If indexInArray(myStrings, "hello") <> -1 Then
                MessageBox.Show("The string in contained within the array")
            End If
        End Sub
        
        K Offline
        K Offline
        KIDYA
        wrote on last edited by
        #3

        Thanx for reply but in function If theArray(i) = value Then Return i //doesn't return i after finding index value same with value,it return -1 for all values

        D 1 Reply Last reply
        0
        • K KIDYA

          Thanx for reply but in function If theArray(i) = value Then Return i //doesn't return i after finding index value same with value,it return -1 for all values

          D Offline
          D Offline
          Denis J Gonzalez
          wrote on last edited by
          #4

          How exactly are you using the function? It should be :

          indexInArray(the array you are working with, "value to search")

          1 Reply Last reply
          0
          • K KIDYA

            Hello Experts!!! I am declaring array arr() as string assign values to array...now i want to search specific content in that array.Can anybody tell me how will i do this?

            L Offline
            L Offline
            Luc Pattyn
            wrote on last edited by
            #5

            Hi, there is an Array.Find(Of T) method in recent .NET versions. You can give it a predicate that explains what exactly is a find. I haven't used it yet. :)

            Luc Pattyn [Forum Guidelines] [My Articles]


            The quality and detail of your question reflects on the effectiveness of the help you are likely to get. Show formatted code inside PRE tags, and give clear symptoms when describing a problem.


            1 Reply Last reply
            0
            • K KIDYA

              Hello Experts!!! I am declaring array arr() as string assign values to array...now i want to search specific content in that array.Can anybody tell me how will i do this?

              D Offline
              D Offline
              dan sh
              wrote on last edited by
              #6

              Array.Contains method.

              1 Reply Last reply
              0
              Reply
              • Reply as topic
              Log in to reply
              • Oldest to Newest
              • Newest to Oldest
              • Most Votes


              • Login

              • Don't have an account? Register

              • Login or register to search.
              • First post
                Last post
              0
              • Categories
              • Recent
              • Tags
              • Popular
              • World
              • Users
              • Groups