This is reasonably easy in .NET 4, with the Zip
method:
using System;
using System.Linq;
class Test
{
static void Main()
{
string a = "0101000000110110000010010011";
string b = "0101XXXXXXX101100000100100XX";
var equal = !(a.Zip(b, (x, y) => new { x, y })
.Where(z => z.x != z.y && z.x != 'X' && z.y != 'X')
.Any());
Console.WriteLine(equal);
}
}
This basically zips the two strings together (considering them as sequences of characters) so we end up with sequences of pairs. We then try to find any pair where the values are different and neither value is 'X'. If any such pair exists, the strings are non-equal; otherwise they're equal.
EDIT: Thinking about it further, we can reverse the predicate and use All
instead:
var equal = a.Zip(b, (x, y) => new { x, y })
.All(z => z.x == z.y || z.x == 'X' || z.y == 'X');
If you're not using .NET 4, you could use the MoreLINQ implementation of Zip which would basically allow you to do the same thing.
Alternatively, you could zip the strings with their indexers like this:
var equal = Enumerable.Range(0, a.Length)
.Select(i => new { x = a[i], y = b[i] })
.All(z => z.x == z.y || z.x == 'X' || z.y == 'X');
That feels like it's cheating somewhat, but it works. Note that in all of these examples, I've assumed that you've already checked whether the input strings are the same length.