How to test if a string contains an item within a list?

pookie

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

Tim Schmelter

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); 

Collected from the Internet

Please contact [email protected] to delete if infringement.

edited at
0

Comments

0 comments
Login to comment

Related

how can I get the index of the item in a list that contains the string being search (within the list) in python

How to loop through each row within a dataframe that contains a string, and match such string to each item in a list?

How to use startsWith and contains within a List of String

Python: How can I check if an item in a list contains a string within an elif statement where two conditions must be met?

How to test string contains one of the substrings in a list, in pandas?

How to test if a string contains one of the substrings in a list, in pandas?

How to test if a string contains one of the substrings stored in a list column in pandas?

How to separate an item that contains in a list?

Manipulating each item (string) in a list, within a list, within a list

Cleaning data: How to iterate through a list find if item contains a string, whitespace or blank and delete that item in Python

How to test if a bashrc contains a string

How to find all occurrences of a string within a list when string is only part of the list item

how to test if array contains item, as is, not some subset of it

How to test assert whether two lists of dictionaries (where a dict item contains a list) are the same

How to test if a list contains another list?

How to test if string contains another string in PHPUnit?

how to check item in a list contains in python

Hamcrest: test list contains an item that has a private field with a certain value

How to check if a string contains item(s) from a list and then add the existing values to a variable in python?

Check if a Python list item contains a string inside another string

How to find keys in a Dict<int,List<Tuple<string,string>>> such that the list contains elements with given Item1 and Items

How to find item within List<T>?

In Python, how might one replace a word within a string with a random item from a list?

Contains within List of doubles

How to replace an item in a list with a dataframe column if column contains the item

How to cast a string that contains a list to <Class 'List'>

Using C# Linq to test if string contains parts of a list of string

How to test if a string contains one of multiple substrings?

How to test that staticTexts contains a string using XCTest

TOP Ranking

HotTag

Archive