0

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.

Community
  • 1
  • 1
Ben
  • 2,518
  • 4
  • 18
  • 31

5 Answers5

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

spender
  • 117,338
  • 33
  • 229
  • 351
  • 1
    Not sure why this was downvoted. I like this approach over spoon feeding. +1 – Sriram Sakthivel Sep 10 '14 at 09:28
  • 2
    @SriramSakthivel : Yep. ["Give a man a fish"](http://en.wiktionary.org/wiki/give_a_man_a_fish_and_you_feed_him_for_a_day;_teach_a_man_to_fish_and_you_feed_him_for_a_lifetime) always seems like a better approach to me. It seems some people just want the fish right now! – spender Sep 10 '14 at 09:30
  • 1
    I wish I could +1 you once more for the quote :) – Sriram Sakthivel Sep 10 '14 at 09:32
  • This was the piece of information I was missing thanks. – Ben Sep 10 '14 at 09:34
  • 2
    You cannot use `Contains` for a case insensitive string comparison. `s1.Contains(str, StringComparer.CurrentCultureIgnoreCase)` won't compile if that is what you suggest. – Tim Schmelter Sep 10 '14 at 09:35
  • @TimSchmelter It will, if you include [this answer](http://stackoverflow.com/a/444818/2530848). I believe that's what OP is talking about – Sriram Sakthivel Sep 10 '14 at 09:36
  • @TimSchmelter : You're right. I'm sure there's enough info here for OP to proceed though. – spender Sep 10 '14 at 09:37
2

You can simply use a lambda expression instead:

if (
     split2.All
     (str => s1.IndexOf(str, StringComparison.CurrentCultureIgnoreCase) != -1)
   )
Luaan
  • 62,244
  • 7
  • 97
  • 116
0

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))
Novakov
  • 3,055
  • 1
  • 16
  • 32
0

You can use the overload of String.IndexOf which accepts a StringComparison:

bool containsAll = split2
    .All(s2 => s1.IndexOf(s2, StringComparison.CurrentCultureIgnoreCase)>= 0);
Tim Schmelter
  • 450,073
  • 74
  • 686
  • 939
-1

Spender was right but far short:

split2.All(str => s1.ToUpper().Contains(str.ToUpper()))

is what you need.

Peter Krassoi
  • 571
  • 3
  • 11
  • This wont compile, you should also avoid ToUpper or ToLower to compare case insensitively.http://stackoverflow.com/a/234751/284240 It is also less efficient since it needs to create many temporary strings. – Tim Schmelter Sep 10 '14 at 19:46
  • You are right, Add->All, add () and ) to the code. If performance is a real issue, don't use string at all. – Peter Krassoi Sep 11 '14 at 14:07
  • You can always edit your answer. Performance is not the only thing, there are cultures where you get an unexpected result if you convert a string to upper-/lowercase. For example: http://blog.codinghorror.com/whats-wrong-with-turkey/ So it's better to always use `StringComparison.CurrentCultureIgnoreCase` instead. For a sub-string search you have to use `s1.IndexOf(s2, StringComparison.CurrentCultureIgnoreCase)>= 0`. – Tim Schmelter Sep 11 '14 at 14:08
  • I didn't see this option. Thx. – Peter Krassoi Sep 11 '14 at 14:09