I have two strings
Like
"0101000000110110000010010011" and
"0101XXXXXXX101100000100100XX"
it should compare each character and it should not consider if the character is X
开发者_Go百科for the above two strings the result is true.
now i'm using like
Iterating through the length of the string and replacing thecorresponding character in first string with X
is there any way to do this using LINQ
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.
You could create a custom comparer and then use the SequenceEqual
method:
string s1 = "0101000000110110000010010011";
string s2 = "0101XXXXXXX101100000100100XX";
bool areEqual = s1.SequenceEqual(s2, new IgnoreXEqualityComparer()); // True
// ...
public class IgnoreXEqualityComparer : EqualityComparer<char>
{
public override bool Equals(char x, char y)
{
return (x == 'X') || (y == 'X') || (x == y);
}
public override int GetHashCode(char obj)
{
throw new NotImplementedException();
}
}
This should work.
var a1 = "0101000000110110000010010011";
var a2 = "0101XXXXXXX101100000100100XX";
var notmatched = a2.Select((cha, idx) =>
{
if (cha != 'X')
return (cha == a1[idx]) ? true : false;
else
return true;
}).Any(x => x == false);
if (notmatched)
//strings are not match
else
//strings are match
If you didn't have the X's, I would know the way. With the X's however, you can't do this with Linq as fas as I'm aware.
Anyway, just make them char arrays and do:
arrayOne.Distinct(arrayTwo).ToArray().Length == 0
Edit: Just occured to me, you can check if that result contains only X's. If it does, return true.
精彩评论