简体   繁体   中英

How to remove elements of a list where its string contains sub strings from another list

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.

您可以使用Any进行以下操作:

strList.RemoveAll(x => describers.Any(d => x.Contains(d)));

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM