List.Contains() doesn't detects a string

前端 未结 3 646
眼角桃花
眼角桃花 2021-01-25 09:44

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

3条回答
  •  滥情空心
    2021-01-25 09:56

    When you do Contains on a List 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.Contains on the entire list, which is what you where doing. Doing a List.Contains(T t1) is the same as doing a Object.Equals(T t1, T listItem) on each item in the list.

提交回复
热议问题