+ Reply to Thread
Results 1 to 6 of 6

Most efficient way to find string in array members

  1. #1
    Forum Expert davegugg's Avatar
    Join Date
    12-18-2008
    Location
    WI, US
    MS-Off Ver
    2010
    Posts
    1,884

    Most efficient way to find string in array members

    Hi

    I'm wondering what the most efficient way to find out if a string is in an array.

    For example, lets say I have an array of:
    arr(0) = "me"
    arr(1) = "you"
    arr(2) = "them"
    arr(3) = "no one"

    I want to find out if "anyone" is in the array. I know I could loop through each element and compare the two strings. Is there a better way?
    Last edited by davegugg; 07-06-2010 at 12:08 PM.
    Is your code running too slowly?
    Does your workbook or database have a bunch of duplicate pieces of data?
    Have a look at this article to learn the best ways to set up your projects.
    It will save both time and effort in the long run!


    Dave

  2. #2
    Forum Expert Bob Phillips's Avatar
    Join Date
    09-03-2005
    Location
    Wessex
    MS-Off Ver
    Office 2003, 2010, 2013, 2016, 365
    Posts
    3,284

    Re: Most efficient way to find string in array members

    Please Login or Register  to view this content.

  3. #3
    Forum Expert davegugg's Avatar
    Join Date
    12-18-2008
    Location
    WI, US
    MS-Off Ver
    2010
    Posts
    1,884

    Re: Most efficient way to find string in array members

    Looks good, but can I use it for partial matches? For instance, if I use "he" for the first argument, it will find it in the element in arr(2) and point to that position? Thus it would work similar to the instr() function, but on all elements of the array.

  4. #4
    Forum Expert shg's Avatar
    Join Date
    06-20-2007
    Location
    The Great State of Texas
    MS-Off Ver
    2003, 2010
    Posts
    40,678

    Re: Most efficient way to find string in array members

    Please Login or Register  to view this content.
    Entia non sunt multiplicanda sine necessitate

  5. #5
    Forum Expert davegugg's Avatar
    Join Date
    12-18-2008
    Location
    WI, US
    MS-Off Ver
    2010
    Posts
    1,884

    Re: Most efficient way to find string in array members

    As the guys in the Guiness commercial say:
    "Brilliant!"

    Thanks to you both.

  6. #6
    Forum Expert davegugg's Avatar
    Join Date
    12-18-2008
    Location
    WI, US
    MS-Off Ver
    2010
    Posts
    1,884

    Re: Most efficient way to find string in array members

    Follow-Up Question: Lets say I want the wild-card to be included on the array end, something like:

    Please Login or Register  to view this content.
    Clearly the above doesn't work. I tried building the array with the wildcard characters in each element, but it didn't seem to work. Any suggestions?

+ Reply to Thread

Thread Information

Users Browsing this Thread

There are currently 1 users browsing this thread. (0 members and 1 guests)

Bookmarks

Posting Permissions

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

Search Engine Friendly URLs by vBSEO 3.6.0 RC 1