问题
I have a string like this:
var str = "DAVID CORPORATION"
then i have a list of substrings that i dont want in the str.
var describers = new List<string> {"CORP", "INC", "LTD", "TECH", "ENGINEER", "LLC","MICROELE"};
then i split the str here into a list:
var strList = str.Split(' ').ToList();
now i want to remove all items of that list that contains the substrings in describers. I found this way to do it a million times all over the internet.
strList.RemoveAll(x => describers.Contains(x));
This does not work because all it does is check if the describers contain the whole word of the strList. I need it to work in reverse.
This doesn't work but its an algorithm of how i want it to work.
strList.RemoveAll(x => x.Contains(describers.Any()));
Cannot convert from 'bool' to 'string'
of course, but how to i remove the item in strList that contains the substring item from describers.
..and only in a linq.lamba. I am trying to stay away foreach/for/do loops.
回答1:
You can do the following using Any
:
strList.RemoveAll(x => describers.Any(d => x.Contains(d)));
来源:https://stackoverflow.com/questions/45990444/how-to-remove-elements-of-a-list-where-its-string-contains-sub-strings-from-anot