Results 1 to 8 of 8

Thread: Shortest string in a collection

  1. #1

    Thread Starter
    New Member
    Join Date
    Feb 2012
    Posts
    8

    Shortest string in a collection

    Hi Forum,

    I am trying to find the most efficient way to find the shortest string in a collection, and by shortest I mean the smallest index of. Any ideas on what would be the least resource intensive way to do this?

  2. #2
    Super Moderator Shaggy Hiker's Avatar
    Join Date
    Aug 2002
    Location
    Idaho
    Posts
    40,104

    Re: Shortest string in a collection

    Could you give an example? I was following up to where you defined shortest to mean something other than "the smallest number of characters", which is the typical definition of short when it comes to strings.

    Also, which language are you talking about?
    My usual boring signature: Nothing

  3. #3

    Thread Starter
    New Member
    Join Date
    Feb 2012
    Posts
    8

    Re: Shortest string in a collection

    Yeah, attached is a screenshot of my GUI, I am using MS Visual Studio, I am wanting to search through the listbox for the smallest (shortest) string and move it into the search result textbName:  Capture.PNG
Views: 133
Size:  19.5 KBox.

  4. #4
    PowerPoster dilettante's Avatar
    Join Date
    Feb 2006
    Posts
    24,487

    Re: Shortest string in a collection

    Well we're all using Visual Studio, VB6 being a subset of VS 6 just as VB5 was part of VS97.

    But it smells like some language and version of .Net based on the icon.

  5. #5
    Super Moderator Shaggy Hiker's Avatar
    Join Date
    Aug 2002
    Location
    Idaho
    Posts
    40,104

    Re: Shortest string in a collection

    So, the index of the shortest string?

    There are ways to do it that use less code, but they are probably slower than the old fashioned way:

    Code:
    dim minIndex as integer
    dim minLength as integer = Integer.Max
    
    For x = 0 to listbox.Items.Count -1
     if listbox.Items(x).Length < minLength Then
      minLength = listbox.Items(x).Length
      minindex=x
     End If
    Next
    My usual boring signature: Nothing

  6. #6

    Thread Starter
    New Member
    Join Date
    Feb 2012
    Posts
    8

    Re: Shortest string in a collection

    Oh duh I am sorry, I am using VB 6, and thank you Shaggy Hiker, that seems to do it the quickest!

  7. #7
    Super Moderator jmcilhinney's Avatar
    Join Date
    May 2005
    Location
    Sydney, Australia
    Posts
    111,221

    Re: Shortest string in a collection

    Quote Originally Posted by uni_panther View Post
    Oh duh I am sorry, I am using VB 6, and thank you Shaggy Hiker, that seems to do it the quickest!
    Are you really? If so then Shaggy's code is not going to work because it's written for VB.NET. That doesn't look like a VB6 icon on that form's title bar either. I think that you need to establish what you're actually using first. That's what the Help -> About menu item is for. Once we've established whether it's VB6 or, as I suspect, VB.NET then you can post to the correct forum in future, rather than General Developer where general development questions belong.
    Why is my data not saved to my database? | MSDN Data Walkthroughs
    VBForums Database Development FAQ
    My CodeBank Submissions: VB | C#
    My Blog: Data Among Multiple Forms (3 parts)
    Beginner Tutorials: VB | C# | SQL

  8. #8
    Super Moderator Shaggy Hiker's Avatar
    Join Date
    Aug 2002
    Location
    Idaho
    Posts
    40,104

    Re: Shortest string in a collection

    Yeah. If my code works, as written, then you aren't using VB6.
    My usual boring signature: Nothing

Posting Permissions

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



Click Here to Expand Forum to Full Width