KrimCard
KrimCard

Reputation: 213

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.

Upvotes: 5

Views: 8906

Answers (1)

Juli&#225;n Urbano
Juli&#225;n Urbano

Reputation: 8488

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

Upvotes: 5

Related Questions