简体   繁体   中英

How to remove a List string element if it contains a string element from another List?

Setup

I have these three lists.

List<List<string>> tokens = new List<string>();
List<string> token = new List<string>();
List<string> sets = new List<string();

One complete token List that would be in the tokens List.

{"<card>"",
 "    <name>Domri Emblem</name>",
 "    <set picURL="http://magiccards.info/extras/token/Gatecrash/Domri-Rade-Emblem.jpg" picURLHq="" picURLSt="">GTC</set>",
 "    <color></color>",
 "    <manacost></manacost>",
 "    <type>Emblem</type>",
 "    <pt></pt>",
 "    <tablerow>0</tablerow>",
 "    <text></text>",
 "    <token>1</token>",
 "</card>"}

The sets list would look like this.

{"ARB", ..., "AVR", ..., "GTC", ..., "ZEN"}

I want to go through each token in tokens and remove each string in token that contains any of the elements in set .

Example

The tokens list has a few token elements. One token (say token1) has an element like this.

{..., "    <set picURL="http://magiccards.info/extras/token/Gatecrash/Domri-Rade-Emblem.jpg" picURLHq="" picURLSt="">GTC</set>", ...}

Another token (say token2) has these two elements.

{..., "    <set picURL="http://magiccards.info/extras/token/magic-2012/pentavite.jpg" picURLHq="" picURLSt="">M12</set>",
"    <set picURL="http://magiccards.info/extras/token/player-rewards-2004/pentavite.jpg" picURLHq="" picURLSt="">MI</set>", ...}

Say the sets list was modified to contain only {"ARB", "GTC", "M12"} .

How would I go through each token in tokens and remove string elements that contain any of the string elements in sets ? So, after that process, token1 will not have that element above and token2 will only have the second presented element?

What I Have Tried

This goes through each token in tokens and removes any element in token that contains the string "GTC" .

foreach (var token in tokens)
{
    token.RemoveAll(str => str.Contains("GTC"));
}

I looked through some other questions and found this but it doesn't work.

foreach (var token in tokens)
{
    token.RemoveAll(sets.Contains);
}

Thanks for the help.

foreach (var token in tokens)
{
    token.RemoveAll(str => sets.Any(s => str.Contains(s)));
}

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