[英]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. 然后我有一个在str中不需要的子字符串列表。
var describers = new List<string> {"CORP", "INC", "LTD", "TECH", "ENGINEER", "LLC","MICROELE"};
then i split the str here into a list: 然后我将str拆分成一个列表:
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. 这是行不通的,因为它所做的只是检查描述程序是否包含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. 当然,但是如何删除包含在描述程序中子字符串项的strList中的项。
..and only in a linq.lamba. ..并且仅在linq.lamba中。 I am trying to stay away foreach/for/do loops.
我试图远离foreach / for / do循环。
您可以使用Any
进行以下操作:
strList.RemoveAll(x => describers.Any(d => x.Contains(d)));
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.