0

I have a list which contains values such as:

  • readies action
  • uses action
  • begins to cast action

I want to compare a string to match against this list and return a boolean based on that. So for example:

string compare = "The Manipulator uses action";

To do this I have tried two different methods, the first being:

if (mylist.Contains(compare)) { //True }

and the second (with Linq):

if (mylist.Any(str => str.Contains(compare))) { //True }

The problem I'm having is both of these methods match against an extended string. What I mean by that is if compare == "uses action" the statement returns true, but if compare == "The Manipulator uses action" the statement returns false.

Any help on fixing this would be most appreciated! :)

1 Answer 1

1

I'm not totally following what you're looking for, so there are 2 different solutions, besed on expected outcome.

If you're only trying to match exactly the same string from within the list Any is the way to go, but you should use == instead of Contains:

if (mylist.Any(str => str == compare)) { //True }

If you'd like extended string to also match, you use Contains, but instead of calling str.Contains(compare) call compare.Contains(str):

if (mylist.Any(str => compare.Contains(str))) { //True }
Sign up to request clarification or add additional context in comments.

2 Comments

Excellent. if (mylist.Any(str => compare.Contains(str))) { //True } does what I was looking for! Thanks so much.
@user1772824 If the answer is what you are looking for, you should accept it.

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.