It is also relatively simple to search for the location where pattern appears in the byte array of data. Of course, this function can be expanded here, for example, to ignore case sensitivity and prevent exceptions.
Public static List <int> indexOf (byte [] data, byte [] pattern)
{
List <int> matchedPos = new List <int> ();
If (data. Length = 0 | data. Length <pattern. Length) return matchedPos;
Int end = data. Length-pattern. Length;
Bool matched = false;
For (int I = 0; I <= end; I ++)
{
For (int j = 0; j <pattern. Length |! (Matched = (j = pattern. Length); j ++)
{
If (data [I + j]! = Pattern [j]) break;
}
If (matched)
{
Matched = false;
MatchedPos. Add (I );
}
}
Return matchedPos;
}
The returned result is a List <int>. If the returned value is 0, the search fails.
Pay attention to the usage of the inside for (;). Generally, when the end is determined, the corresponding Boolean value is considered correct and the bypass of "|" is considered, that is, the right statement is executed only when "|" is False on the left side of the for statement on the inner side. Therefore, I reset the Boolean value in the right statement, then, determine whether the location matches.