Solved

Pulling items out of an Array

Posted on 2004-10-18
3
236 Views
Last Modified: 2010-04-24
If I have MyArray(100) and wanted to pull out all elements that begin with a particular letter, how would that be done?  
0
Comment
Question by:Jedidia
  • 2
3 Comments
 
LVL 85

Accepted Solution

by:
Mike Tomlinson earned 125 total points
ID: 12340007
Here is one approach:

Public Class Form1
    Inherits System.Windows.Forms.Form

    Private myArray() As String

    Private Sub Form1_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
        ' Put some test data in our array
        Dim temp As String
        temp = "cat,dog,elephant,cougar,crab,bear,fish,camel"
        myArray = Split(temp, ",")
    End Sub

    Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click
        Dim match As String
        Dim matches As ArrayList = getEntries("c")

        For Each match In matches
            ' do something with each match
            Debug.WriteLine(match)
        Next
    End Sub

    Private Function getEntries(ByVal beginningWith As String) As ArrayList
        Dim entries As ArrayList = New ArrayList
        Dim i As Integer

        For i = 0 To myArray.GetUpperBound(0)
            If myArray(i).StartsWith(beginningWith) Then
                entries.Add(myArray(i))
            End If
        Next i

        Return entries
    End Function

End Class
0
 
LVL 4

Expert Comment

by:gdexter
ID: 12340116
This Function will return an ArrayList containing the elements starting with the searchParam variable

    Private Function GetElements(ByVal searchParam As String) As ArrayList

        Dim i As Integer
        Dim al As New ArrayList

        For i = 0 To MyArray.GetUpperBound(0)
            If (CType(MyArray(i), String).StartsWith(searchParam)) Then
                al.Add(MyArray(i))
            End If
        Next

        Return al
    End Function
0
 
LVL 4

Expert Comment

by:gdexter
ID: 12340131
To late...
0

Featured Post

VMware Disaster Recovery and Data Protection

In this expert guide, you’ll learn about the components of a Modern Data Center. You will use cases for the value-added capabilities of Veeam®, including combining backup and replication for VMware disaster recovery and using replication for data center migration.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Suggested Solutions

It’s quite interesting for me as I worked with Excel using vb.net for some time. Here are some topics which I know want to share with others whom this might help. First of all if you are working with Excel then you need to Download the Following …
Parsing a CSV file is a task that we are confronted with regularly, and although there are a vast number of means to do this, as a newbie, the field can be confusing and the tools can seem complex. A simple solution to parsing a customized CSV fi…
Established in 1997, Technology Architects has become one of the most reputable technology solutions companies in the country. TA have been providing businesses with cost effective state-of-the-art solutions and unparalleled service that is designed…

823 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question