4
\$\begingroup\$

I have a string of numbers, like "31654918562314", and I want to convert it into an array of integers. The code I've written is:

string source = "31654918562314";
int[] numbers = new int[source.Length];
for (int i = 0; i < source.Length; i++)
{
    numbers[i] = Convert.ToInt32(source.Substring(i, 1));
}

What improvements can I apply on this code? Is it the best ultimate code I can get to?

Note: I'm using C#. However, I also should implement this code in JavaScript.

\$\endgroup\$
5
  • \$\begingroup\$ One thing that jumps out at me is that you can just subtract the character value of 0 from each digit, rather than call Convert.ToInt32() or Int32.Parse(). \$\endgroup\$
    – Jon Purdy
    Commented Oct 15, 2011 at 16:47
  • \$\begingroup\$ You've got a point there @Jon, slipped my mind. \$\endgroup\$ Commented Oct 15, 2011 at 17:43
  • 2
    \$\begingroup\$ I wouldn't subtract the character value of 0 from each digit, there is no guarantee that in the future the character values for the digits will be sequential. You might want to look at Char.GetNumericValue msdn.microsoft.com/en-us/library/e7k33ktz.aspx \$\endgroup\$
    – Joey
    Commented Oct 18, 2011 at 8:54
  • \$\begingroup\$ @Joey: >.< No! There is a guarantee that the char values will be sequential. It's been this way for 49 years, first in ASCII and now in Unicode, and it isn't going to change. \$\endgroup\$
    – amara
    Commented Apr 6, 2012 at 18:46
  • \$\begingroup\$ That's no guarantee, that is merely an observation and an assertion. It's akin to saying, "stocks have risen for the past x years and it isn't going to change"... Admittedly it is unlikely to change but why take the risk when you can let someone else worry about it, in this case Microsoft. \$\endgroup\$
    – Joey
    Commented Apr 10, 2012 at 9:04

3 Answers 3

6
\$\begingroup\$

I'm partial to using LINQ here. Jon has a point there on the conversion.

var str = "31654918562314";
var numbers = str.Select(c => c - '0').ToArray();
\$\endgroup\$
5
\$\begingroup\$

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

\$\endgroup\$
1
  • \$\begingroup\$ see @Joey's comment above \$\endgroup\$
    – jfs
    Commented Dec 22, 2011 at 3:34
4
\$\begingroup\$

Javascript

var arr = [];
var matches = "54321".match(/\d/g);
for (var index = 0; index < matches.length; ++index) {
    arr.push(parseInt(matches[index], 10));
}

live demo

Using map() method:

var arr = "54321".match(/\d/g).map(function(m){
    return parseInt(m, 10);
});

live demo

\$\endgroup\$
1
  • \$\begingroup\$ Note that accessing a string like an array doesn't work in some older browsers. \$\endgroup\$
    – Guffa
    Commented Oct 18, 2011 at 7:45

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.