简体   繁体   中英

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.

You have to use String.Split to get a collection that you can use:

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:

bool containsAll = CommaSeparatedStrings.Split(',').All(StringList.Contains);

检查CommaSeperatedStrings是否已CommaSeperatedStrings StringList包含任何值

bool contains = !CommaSeperatedStrings.Split(',').Except(StringList).Any()

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM