As the title says my list \"Cvqt\" contains different strings for example \"1 maxHearts Player\"
and I\'m trying to check if it contains \" maxHearths \" + na
When you do Contains
on a List<string>
it only checks if the list contains a string that is a exact match to what you are passing in, it will not do a partial match.
If you want partial matches you need to do something more complicated like
if (Cvqt.Any(x=>x.Contains(" maxClubs " + name)))
{
Cvqt.Remove(maxClubs.ToString() + " maxClub " + name);
}
or
var item = Cvqt.FirstOrDefault(x => x.Contains(" maxClubs " + name));
if (item != null)
{
Cvqt.Remove(item);
}
if you want to remove the found item instead of the specific string using maxClubs
.
What those two methods do is call String.Contains on each item instead of List<T>.Contains on the entire list, which is what you where doing. Doing a List<T>.Contains(T t1)
is the same as doing a Object.Equals(T t1, T listItem)
on each item in the list.
Contains as it pertains to a List object looks for the entire string in a list of strings.
You are looking for "max clubs" as a substring. That is the "Contains" off the string object
Example:
List<string> x = new List<string>(){"Hello", "goodbye"};
bool y = x.Contains("good"); // will be false
y = x.Contains("goodbye"); // will be true
y = x[0].Contains("Hell"); // will be true
You're probably looking for something like this:
List<string> stringsWithMaxClubs = Cvqt.Where(argString => argString.Contains(" maxClubs " + name)).ToList();
Your list contains strings in the form of maxClubs + " maxClubs " + name
while you're searching for " maxClubs " + name
. There will be no element in your list that exactly matches this (unless maxClubs
is an empty string).
You can use Linq Any for what you want:
if (Cvqt.Any(x => x.Contains(" maxClubs " + name)))
{
}