Results 1 to 8 of 8

Thread: Shortest string in a collection

  1. #1
    New Member
    Join Date
    Feb 12
    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
    Loquacious User Shaggy Hiker's Avatar
    Join Date
    Aug 02
    Location
    Idaho
    Posts
    20,390

    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
    New Member
    Join Date
    Feb 12
    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: 25
Size:  19.5 KBox.

  4. #4
    PowerPoster
    Join Date
    Feb 06
    Posts
    8,564

    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
    Loquacious User Shaggy Hiker's Avatar
    Join Date
    Aug 02
    Location
    Idaho
    Posts
    20,390

    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
    New Member
    Join Date
    Feb 12
    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
    .NUT jmcilhinney's Avatar
    Join Date
    May 05
    Location
    Sydney, Australia
    Posts
    80,747

    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.

  8. #8
    Loquacious User Shaggy Hiker's Avatar
    Join Date
    Aug 02
    Location
    Idaho
    Posts
    20,390

    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
  •