2

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.

1
  • What should be the return value for "Admin,Cook"? Commented Dec 10, 2014 at 13:08

2 Answers 2

6

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);
Sign up to request clarification or add additional context in comments.

Comments

1

check if CommaSeperatedStrings splitted - StringList contains any value

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

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.