5

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.

4
  • Side note - your sample looks like XML... Please consider using XML APIs instead if it is indeed XML. Commented Apr 22, 2013 at 1:07
  • @AlexeiLevenkov I didn't know about any XML APIs but hindsight makes it seem obvious they would exist. Thanks, I will look into them. Commented Apr 22, 2013 at 1:13
  • Starting point - search for C# XDocument - there are plenty examples on MSDN and many question on this site. Commented Apr 22, 2013 at 1:44
  • @AlexeiLevenkov A brief search earlier pointed me to only loading an XML but those keywords gave me more. Particularly, getting information found between tags. In my case, anything between the <set> tags and other easier ways to extract more information from my XML file. I really should look for (and study) APIs before I try my unwieldy approaches. Thanks again, this time for the guidance. Commented Apr 22, 2013 at 2:47

1 Answer 1

5
foreach (var token in tokens)
{
    token.RemoveAll(str => sets.Any(s => str.Contains(s)));
}
Sign up to request clarification or add additional context in comments.

1 Comment

That works. Thank you. Will mark as answer at the end of the time limit.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.