I came from this question but there are also possible invalid values within my string. e.g.
string input = "1;2;3;4;5;6x;7;8;9;1x0";
should result into [1,2,3,4,5,7,8,9]
, because ["6x", "1x0"]
are invalid integer values
My approach: https://dotnetfiddle.net/Ji4bzq
string i = "1;2;3;4;5;6x;7;8;9;1x0";
int temp = -1;
int[] r = i.Split(';').Where(x => int.TryParse(x, out temp)).Select(_ => temp).ToArray();
which seems to work but feels kinda wrong because of that Select(_ => temp)
part.
Question: Is there a better way in terms of readability and reliability? (AsParallel
should fail here)