Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 6994167
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 27, 20262026-05-27T19:51:28+00:00 2026-05-27T19:51:28+00:00

say, i have a string array of 50000 elements. Searching the array using For

  • 0

say, i have a string array of 50000 elements. Searching the array using For Next is so slow for such a huge array. Is there any fast way to search?

Note: Using join & instr we can search for a string in an array, but this method is no good as i can not find out the element number

Note: the array is unsorted. And i’m looking for substrings

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-27T19:51:29+00:00Added an answer on May 27, 2026 at 7:51 pm

    This is an expansion of your idea to use Join and InStr:

    Sub TestArraySearch()
    Dim A(4) As String
        A(0) = "First"
        A(1) = "Second"
        A(2) = "Third"
        A(3) = "Fourth"
        A(4) = "Fifth"
        Debug.Print FastArraySearch(A, "Fi")
        Debug.Print FastArraySearch(A, "o")
        Debug.Print FastArraySearch(A, "hird")
        Debug.Print FastArraySearch(A, "Fou")
        Debug.Print FastArraySearch(A, "ndTh")
        Debug.Print FastArraySearch(A, "fth")
    End Sub
    
    Function FastArraySearch(SearchArray As Variant,SearchPhrase As String) As String
    Dim Pos As Long, i As Long, NumCharsProcessed As Long, Txt As String
        Pos = InStr(Join(SearchArray, "§"), SearchPhrase)
        If Pos > 0 Then
            For i = LBound(SearchArray) To UBound(SearchArray)
                NumCharsProcessed = NumCharsProcessed + Len(SearchArray(i)) + 1
                If NumCharsProcessed >= Pos Then
                    FastArraySearch = SearchArray(i)
                    Exit Function
                End If
            Next i
        End If
    End Function
    

    I did not benchmark it, but it should be quicker than doing a separate search each time through the loop. It searches once, then just adds up the string lengths until it gets to where the match was. Because the length of the string is stored before any of the characters in the string, the Len function is highly optimized.

    If this performance is still unacceptable I think you will need to find a different data structure than an array (eg, a disconnected recordset, as @Remou suggested).

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

Say I have an array of values: string[] text = new string[] { val1,
Say I have an array of strings: string[] strArray = {aa, bb, xx, cc,
Lets say I have an array like this: string [] Filelist = ... I
I have a string say string s =C:\\Data , I have an array which
Let's say I have this Hello.scala. object HelloWorld { def main(args: Array[String]) { println(Hello,
let's say I have this string array in java String[] test = {"hahaha lol",
Let's say I have a string array of field internal names. How do I
Let's say I have game.abc as a string (array of chars) and I want
Let's say I have array like {12,23,,34,22,,,12} If there are 5 items as than
Let say I have an array: string[] s = GetArray(); and method: public bool

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.