After using the String.Split()
method to split the String, you can use the Convert.ToUInt16()
method inside the try-catch block to safely convert the String
data to UInt16
type. Developed static methods are available below:
public static UInt16[] Convert(String[] values)
{
UInt16[] numbers = new UInt16[values.Length - 1];
try
{
for(int i = 0 ; i < values.Length - 1 ; ++i)
numbers[i] = System.Convert.ToUInt16(values[i]);
}
catch(FormatException){}
return numbers;
}
public static void Print(UInt16[] numbers)
{
for(int index = 0 ; index < numbers.Length ; ++index)
System.Console.WriteLine("[{0}]: {1}", index, numbers[index]);
}
Here is the application program that calls the static methods:
String[] values= "1;2;3;4;5;".Split( ';' );
uint size = (uint)values.Length - 1;
UInt16[] numbers = new UInt16[size];
numbers = Convert(values);
Print(numbers);
This program produces the following output:
[0]: 1
[1]: 2
[2]: 3
[3]: 4
[4]: 5