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?
Printable View
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?
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?
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 textbAttachment 92551ox.
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.
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
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.
Yeah. If my code works, as written, then you aren't using VB6.