Skip to main content

Notice

Please note that most of the software linked on this forum is likely to be safe to use. If you are unsure, feel free to ask in the relevant topics, or send a private message to an administrator or moderator. To help curb the problems of false positives, or in the event that you do find actual malware, you can contribute through the article linked here.
Topic: Easy way to compare array elements in C# (Read 52733 times) previous topic - next topic
0 Members and 1 Guest are viewing this topic.

Easy way to compare array elements in C#

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?

Easy way to compare array elements in C#

Reply #1
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 methodsContains and IndexOf look to do what you're essentially trying to do.

EDIT: Added more ideas.


Easy way to compare array elements in C#

Reply #3
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.aspx

Edit: typo

Easy way to compare array elements in C#

Reply #4
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.

Easy way to compare array elements in C#

Reply #5
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.

Easy way to compare array elements in C#

Reply #6
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.

Easy way to compare array elements in C#

Reply #7
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: [Select]
if (first.Length <= second.Length)
{
for (int x = 0; x < first.Length; x++)
{
  if (first[x] != second[x]) return false;
}
return true;
}
return false;
Nothing is impossible if you don't need to do it yourself.