-1

Say I have a string:

var s = "z_cams_c_ecmf_20160704120000_prod_fc_ml_012_go3.nc";

and say I have a list of strings:

var items = new List<string>(){"fc", "an"};

How can I test if s contains either fc or an?

I don't want to loop over items and test each case because I am looking for multiple conditions. For example, I will also want to test if s contains "prod" or "rean" or "test".

I suppose I could throw all my conditions (fc, an, prod, rean, test, etc...) into single list and iterate over each of them, but it doesn't feel right, given the user must supply JSON which is deseralized into an object with various lists for condition matching.

I suppose I am looking for something like the answer seen here, but I do not know what Mdd LH is...

pookie
  • 3,796
  • 6
  • 49
  • 105
  • `Any` and `Contains` also loops over the lists that they are applied to. How do you expect to validate each element in the list if you're not able to loop over it? – default Jul 06 '17 at 09:33
  • Regarding your linked answer; "Mdd LH" is just a string. That answer checks if any string in `myList` contains "Mdd LH". I believe that it was used as an example, nothing more. – default Jul 06 '17 at 09:36

1 Answers1

3

LINQ also uses loops, but you don't see them ;-)

bool contains = items.Any(s.Contains);

If you want to ignore the case(so upper or lowercase letters):

contains = items.Any(i => s.IndexOf(i, StringComparison.InvariantCultureIgnoreCase) >= 0); 
Tim Schmelter
  • 450,073
  • 74
  • 686
  • 939