简体   繁体   English

如何从列表中删除其字符串包含子字符串的列表元素

[英]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.

相关问题 如何从另一个列表中删除列表中的字符串? - How to remove strings in list from another list? 如果列表字符串元素包含另一个列表中的字符串元素,如何删除? - How to remove a List string element if it contains a string element from another List? LINQ选择列表,其中子列表包含来自另一个列表的项目 - LINQ select List where sub-list contains item from another list 如何从列表中删除特定的字符串<strings> - How to remove a specific string from list<strings> 如何迭代子列表中的元素,然后从列表中删除子列表? 以出色的表现 - How to iterate at elements from a sub list and then remove the sub list from the list? With great performance 确定List是否包含来自另一个List的元素 - Determine if a List contains elements from another List 如何从列表中删除零个元素 <string> ? - How to remove zero elements from List<string>? Linq过滤器列表<string>它包含来自另一个列表的字符串值<string></string></string> - Linq filter List<string> where it contains a string value from another List<string> 检查字符串列表是否包含对象属性值,是否将其从字符串列表中删除 - Check if list of string contains object property value, and if it does remove it from the list of strings 检查字符串是否包含列表(字符串)中的元素 - Check if a string contains an element from a list (of strings)
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM