Reputation: 87
I have a list which contains values such as:
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! :)
Upvotes: 0
Views: 186
Reputation: 125620
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 }
Upvotes: 1