Solved

Pulling items out of an Array

Posted on 2004-10-18
3
227 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

Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

Question has a verified solution.

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

If you're writing a .NET application to connect to an Access .mdb database and use pre-existing queries that require parameters, you've come to the right place! Let's say the pre-existing query(qryCust) in Access takes a Date as a parameter and l…
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…
This demo shows you how to set up the containerized NetScaler CPX with NetScaler Management and Analytics System in a non-routable Mesos/Marathon environment for use with Micro-Services applications.
This tutorial demonstrates a quick way of adding group price to multiple Magento products.

930 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

Need Help in Real-Time?

Connect with top rated Experts

13 Experts available now in Live!

Get 1:1 Help Now