Sebastian Mares
Apr 17 2008, 10:20
I recently started using C# for some personal tools and need some help. Let's say you have the following arrays:
char[] first = { 'H', 'e', 'l', 'l', 'o' };
char[] second = { 'S', 'h', 'e', ' ', 's', 'a', 'i', 'd', ' ', 'H', 'e', 'l', 'l', 'o' };
What would be the best method to check if all characters in first are present in second at the index x?
An example where this would be useful: you read the last 128 characters of a file into an array and then want to check if the first three elements are "TAG". Currently I have an own function where I pass two arrays and then compare each element in a loop. Is there an easier method?
sthayashi
Apr 17 2008, 10:37
I don't know much about C#, but wouldn't
Strstr do the trick? Or since you know the location, create a char *, point it to the location in question, and do a
strncmp on it.
Or if you're using C# strings rather than C strings, use one of the
built-in methods.
Contains and
IndexOf look to do what you're essentially trying to do.
EDIT: Added more ideas.
Sebastian Mares
Apr 17 2008, 23:24
That would work for strings but I wanted something more general that could also compare int arrays for example. I thought .NET might have a function for that already.
hawkeye_p
Apr 18 2008, 00:24
Sebastian,
I'm not sure, wether you want to find the characters of the first string in the given sequence or not. In the first case IndexOf should help, in the second IndexOfAny.
You find more help for these functions at
http://msdn2.microsoft.com/en-us/library/k8b1470s.aspxEdit: typo
niktheblak
Apr 22 2008, 03:54
If your data is plain String instances, you can just use the IndexOf and IndexOfAny methods of the String class.
If you process raw byte arrays and don't want to create String instances of them for efficiency reasons, you're out of luck. There's no subarray search routines for byte arrays in the .NET Framework class library that I'm aware of. You'll have to roll your own. I recommend the Bayer-Moore-Horspool algorithm modified for byte arrays.
Coincidentally, some time ago I was writing a tag scanner software with C#. I had to search for subarrays (for example the string 'TAG') from a raw byte array (an MP3 file). It seems that we have a
very similar use case in mind

I rolled my own code that uses naive substring search for byte arrays. It was good enough efficiency-wise so I left it there.
sthayashi
Apr 22 2008, 06:45
The next best approach, since you want to use this on int arrays as well, would be to use a combination of memcmp & memchr, but you have to worry about the buffer overflow problem.
Actually, doing some search of the MSDN, I found _lfind. That should be what you were looking for.
qristus
Apr 22 2008, 12:58
I second niktheblak's suggestion. I needed something similar a few months back and ended up using Boyer-Moore-Horspool as well. It's fast, and easy to implement. The
wikipedia article currently contains a C implementation of it which should be easy to port.
Silversight
Apr 22 2008, 13:09
Dammit, I wanted to suggest Boyer-Moore-Horspool as well.

Though when checking whether the elements of
first are the first elements of
second, your own function should be sufficient.
CODE
if (first.Length <= second.Length)
{
for (int x = 0; x < first.Length; x++)
{
if (first[x] != second[x]) return false;
}
return true;
}
return false;