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

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

本文介绍了如何从另一个列表中删除其字符串包含子字符串的列表元素的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个像这样的字符串:

I have a string like this:

var str = "DAVID CORPORATION"

然后我在str中有一个我不想要的子字符串列表.

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"};

然后我将str拆分为一个列表:

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));

这不起作用,因为它所做的只是检查描述程序是否包含strList的整个单词.我需要它反向工作.

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()));

当然,但是如何删除strList中包含描述者中子字符串项的项.

of course, but how to i remove the item in strList that contains the substring item from describers.

..,并且仅在linq.lamba中.我试图远离foreach/for/do循环.

..and only in a linq.lamba. I am trying to stay away foreach/for/do loops.

推荐答案

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

You can do the following using Any:

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

这篇关于如何从另一个列表中删除其字符串包含子字符串的列表元素的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

08-15 04:28