The solution given by Mehdi Gholam works, but this alternative also solves your problem.
protected int Checkscore(Node x)
{
char[] delimiter = {';'};
string[] numbers = x.GetProperty("answers").Value.Split(delimiter, StringSplitOptions.RemoveEmptyEntries);
int score = 0;
foreach (string n in numbers)
{
score += int.Parse(n);
LabelMonitor.Text += n + " ";
}
return score;
}
The
StringSplitOptions.RemoveEmptyEntries
[
^] option tells the
Split
[
^] function to remove all empty results.
To make sure the code also works when the string contains non digit characters you should use
Int32.TryParse
[
^].
When using
TryParse
you also deal with the empty result from the
Split
function, so you can use your original call to the
Split
function again.
protected int Checkscore(Node x)
{
string[] numbers = x.GetProperty("answers").Value.Split(';');
int score = 0;
int parseResult = 0;
foreach (string n in numbers)
{
if (Int32.TryParse(n, out parseResult))
score += parseResult;
LabelMonitor.Text += n + " ";
}
return score;
}