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

依然范特西╮ 提交于 2021-02-05 10:46:23

问题


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

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!