[英]How to test if a string contains an item within a list?
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
? 如何测试
s
包含fc
或an
?
I don't want to loop over items
and test each case because I am looking for multiple conditions. 我不想遍历
items
并测试每个案例,因为我正在寻找多个条件。 For example, I will also want to test if s
contains "prod" or "rean" or "test". 例如,我还将要测试
s
包含“ prod”或“ rean”或“ 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. 我想我可以将所有条件(
fc
, an
, prod
, rean
, test
等)放入单个列表并遍历每个条件,但是由于用户必须提供JSON,因此感觉不对。反序列化为具有各种条件匹配列表的对象。
I suppose I am looking for something like the answer seen here , but I do not know what Mdd LH
is... 我想我正在寻找类似此处所示答案的内容,但是我不知道
Mdd LH
是什么...
LINQ also uses loops, but you don't see them ;-) LINQ也使用循环,但是您看不到它们;-)
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);
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.