Afnan Makhdoom Afnan Makhdoom - 3 months ago 50 Question

How to find byte in array of bytes using VB.NET

How can I search within an array of byte for a specific byte?

For example, I use

Instr(String, "something to search")
InstrRev(String, "something to search")
for strings. I basically don't want to loop through the byte array, because I have extremely long byte arrays and I want to search bytes in a flash.

I just need the fastest and the simplest code possible for my task.

What would be a faster quicker and simpler way to search? A byte array of a file, or to stream a file with filestream and then to search within it?


The System.Array class exposes many useful methods when working with arrays.

Dim [array] As Byte() = New Byte() {1, 2, 4, 6, 3}
Dim find As Byte = 3
Dim index As Integer = System.Array.IndexOf(Of Byte)([array], find)

If (index = -1) Then
    'Not found...
End If