try this code:
- try
-
{
-
ushort num;
-
int counter=-1;
-
string line = "1 12 15 12 8 21";
-
string[] str = line.Split(' ');
-
ushort[] Numbers = new ushort[str.Length];
-
foreach(string piece in str)
-
{
-
counter++;
-
if (UInt16.TryParse(piece, out num))
-
{
-
Numbers[counter]=num;
-
}
-
}
-
-
}
-
catch (Exception ex)
-
{
-
MessageBox.Show(ex.Message);
-
}
However mind the following words of caution:
While accessing the array
Numbers you have to first check if the array element contain a value or not.
For example, if you supple the string line as " 2 3 f 4 5"
The Number array will have values like
Numbers[0] = 2
Numbers[1] =3
Numbers[2] =null
Numbers[3] = 4
Numbers[4] = 5
By slightly changing the code, however, you can avoid this problem. Also you can provide a check like:
- if(Numbers[i].ToString()==String.Empty)
All the best,