You first loop will cause an ArgumentOutOfRangeException
if no item in the list matches the string. In this case you will happily walk past the end of the list. Your second loop has the same issue.
Further I think you have to increment count
one more time between the two loops - currently you are adding the item "Attributes;"
to the list TypeAndAmount
. This will also cause an ArgumentOutOfRangeException
because splitting the string "Attributes;"
at spaces yields only one string and you are accessing Type[1]
in your last loop.
I suggest rewriting your code using some LINQ (including the step to skip the item "Attributes;"
).
// Skip everything until we find 'Attributes;'.
var stepOne = Player.PlayerList.SkipWhile(entry => entry != "Attributes;");
// Skip over 'Attributes;'.
var stepTwo = stepOne.Skip(1);
// Take everything until we find '.'.
var stepThree = stepTwo.TakeWhile(entry => entry != ".");
// Stuff everything we selected into a new list.
var typeAndAmount = stepThree.ToList();
You can also put everything into one statement - less verbose but harder to debug.
var typeAndAmount = Player.PlayerList
.SkipWhile(entry => entry != "Attributes;")
.Skip(1)
.TakeWhile(entry => entry != ".")
.ToList();