简体   繁体   中英

VB.net Search for String in Array

trying to figure out how I can search for a string in my arrays. As of now, I only figured out how to search through integers. My array is like this:

Dim IDno() As String = {264, 951, 357}
Dim author() As String = {"Peter", "Nathan", "Sandy"}

So as of now, if someone types in the number 951, my listbox will display:

ID#: 951

Name: Nathan

I want to know how I can allow users to search for names instead and it will display the ID# and the Name.

You can do this using Array.FindIndex - Only if all values/IDs are going to be unique.

Something along the lines of this should do the trick for you.

Dim ind As Integer = Array.FindIndex(IDno, "yourID")
Dim name As String = author(ind)

I've not got VS to hand to test this but off the top of my head, that should work. Although, as I said, it's only going to work if all the ID numbers are unique. (And obviously replace "yourID" with the ID you're using).

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM