24

I have a List<string> that has some items like this:

{"Pre Mdd LH", "Post Mdd LH", "Pre Mdd LL", "Post Mdd LL"}

Now I want to perform a condition that checks if an item in the list contains a specific string. something like:

IF list contains an item that contains this_string

To make it simple I want to check in one go if the list at least! contains for example an item that has Mdd LH in it.

I mean something like:

if(myList.Contains(str => str.Contains("Mdd LH))
{
    //Do stuff
}

Thanks.

Dumbo
  • 13,555
  • 54
  • 184
  • 288

5 Answers5

80

I think you want Any:

if (myList.Any(str => str.Contains("Mdd LH")))

It's well worth becoming familiar with the LINQ standard query operators; I would usually use those rather than implementation-specific methods (such as List<T>.ConvertAll) unless I was really bothered by the performance of a specific operator. (The implementation-specific methods can sometimes be more efficient by knowing the size of the result etc.)

ZombieSheep
  • 29,603
  • 12
  • 67
  • 114
Jon Skeet
  • 1,421,763
  • 867
  • 9,128
  • 9,194
  • How can we make it case insensitive search? Contains doesn't have that overload method. – Ankur-m Oct 07 '15 at 08:38
  • @Ankur-m: You can use `IndexOf` instead, passing in a `StringComparison`. If that's not enough detail, please ask a new question (after searching for existing ones, of course). – Jon Skeet Oct 07 '15 at 08:50
  • I could do it using IndexOf. However I had to use FindIndex instead of Any. I am still looking why Any didn't work. And yes there are similar posts which I couldn't find in my first search. Thank a lot for the help! – Ankur-m Oct 07 '15 at 09:21
  • @Ankur-m: Well if you're still confused, I suggest you ask a new question - if you don't need the index, then Any really should work. – Jon Skeet Oct 07 '15 at 09:22
  • I will create a question then after some more analysis. – Ankur-m Oct 07 '15 at 09:25
  • I was actually able to understand the issue and so didn't create a new question. I used below to get it working: searchText.Any(x => x.IndexOf(eachWord, StringComparison.OrdinalIgnoreCase) >= 0. Before I was using < 0 (because I needed no-match condition) and it always returned 'True'. I should have used == 0. I finally changed it to 'exact word' match by changing the code to: searchText.Any(x => x.IndexOf(string.Format(" {0} ", eachWord), StringComparison.OrdinalIgnoreCase) >= 0) of course by padding spaces at start and end of my input strings. Thanks again! :) – Ankur-m Oct 14 '15 at 10:31
  • Why does it returns true when an empty string if tried to be found in a list of strings where there is no empty string ? i.e. if (list.Any(str => str.Contains(val))) returns true if 'val' is an empty string and 'list' does not contains any empty string. https://dotnetfiddle.net/JnE3Fe – Manik Arora Nov 20 '18 at 05:21
  • 1
    @ManikArora: Because any string "contains" the empty string. That's more about `string.Contains` than about `Any`. See https://learn.microsoft.com/en-us/dotnet/api/system.string.contains?view=netframework-4.7.2 – Jon Skeet Nov 20 '18 at 06:28
14

Thast should be easy enough

if( myList.Any( s => s.Contains(stringToCheck))){
  //do your stuff here
}
Øyvind Bråthen
  • 59,338
  • 27
  • 124
  • 151
9

Try this:

bool matchFound = myList.Any(s => s.Contains("Mdd LH"));

The Any() will stop searching the moment it finds a match, so is quite efficient for this task.

slugster
  • 49,403
  • 14
  • 95
  • 145
7

LINQ Any() would do the job:

bool contains = myList.Any(s => s.Contains(pattern));

Any(), MSDN:

Determines whether any element of a sequence satisfies a condition

sll
  • 61,540
  • 22
  • 104
  • 156
3

If yoou use Contains, you could get false positives. Suppose you have a string that contains such text: "My text data Mdd LH" Using Contains method, this method will return true for call. The approach is use equals operator:

bool exists = myStringList.Any(c=>c == "Mdd LH")