The most efficient code would be to use a plain loop and get the numerical value from the character code:
string source = "31654918562314";
int[] numbers = new int[source.Length];
for (int i = 0; i < source.Length; i++) {
numbers[i] = source[i] - '0';
}
In Javascript you would use the charCodeAt
method to do the same:
var source = '31654918562314';
var numbers = new Array(source.length);
for (var i = 0; i < source.length; i++) {
numbers[i] = source.charCodeAt(i) - 48;
}
A performance test shows that using charCodeAt
in a loop is 10-30 times faster than using a regular expression and parseInt
: http://jsperf.com/string-to-array-of-numbers