[英]Finding comma separated Strings in List<String>
I have a string list and I have a comma separated String. 我有一个字符串列表,并且我有一个逗号分隔的字符串。
I would like to do something like: 我想做类似的事情:
StringList.Contains(CommaSeparatedStrings);
So for example I have a list like: 因此,例如,我有一个类似的列表:
StringList.Add(Admin);
StringList.Add(Nurse);
StringList.Add(Cook);
CommaSeparatedStrings = "Admin,Nurse";
So the above mentioned function should return true. 因此,上述函数应返回true。
You have to use String.Split
to get a collection that you can use: 您必须使用
String.Split
来获取可以使用的集合:
bool containsAny = StringList.Intersect(CommaSeparatedStrings.Split(',')).Any();
If you want to know if all items (not only at least one) are contained: 如果您想知道是否包含所有项目(不仅是至少一项):
bool containsAll = !CommaSeparatedStrings.Split(',').Except(StringList).Any();
or with Enumerable.All
which seems to be the most readable way: 或使用
Enumerable.All
似乎是最易读的方式:
bool containsAll = CommaSeparatedStrings.Split(',').All(StringList.Contains);
检查CommaSeperatedStrings
是否已CommaSeperatedStrings
StringList
包含任何值
bool contains = !CommaSeperatedStrings.Split(',').Except(StringList).Any()
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.