Reputation: 2725
If I have an array and I'm using LINQ to check the array elements contain a string using .All() is there a way to do this being case insensitive?
My code is:
string s1 = "hello my name is blah";
string[] split2 = fund.Split(' ');
if (split2.All((s1.Contains)))
{
//Do something
}
If I was doing a simple .Contains(string) I could use the solution from this question. I think the answer will be roughly the same but I'm unsure how to implement the original solution when using delegates.
Upvotes: 0
Views: 164
Reputation: 567
Spender was right but far short:
split2.All(str => s1.ToUpper().Contains(str.ToUpper()))
is what you need.
Upvotes: -1
Reputation: 460238
You can use the overload of String.IndexOf
which accepts a StringComparison
:
bool containsAll = split2
.All(s2 => s1.IndexOf(s2, StringComparison.CurrentCultureIgnoreCase)>= 0);
Upvotes: 0
Reputation: 120508
split2.All(s1.Contains)
is effectively a shorthand for
split2.All(str => s1.Contains(str))
knowing this, it should now be easy for you to apply the extra parameter that you need.
Upvotes: 4
Reputation: 3095
You can create delegate which wraps Contains
Func<string, bool> insensitiveContains = s => s1.Contains(s, StringComparer.CurrentCultureIgnoreCase);
That you can use insensitiveContains as argument to All():
if (split2.All(insensitiveContains))
Upvotes: 0
Reputation: 63772
You can simply use a lambda expression instead:
if (
split2.All
(str => s1.IndexOf(str, StringComparison.CurrentCultureIgnoreCase) != -1)
)
Upvotes: 2