Convert a string representation of a number to int[] in C#

8

2

What is the shortest C# statement that will convert a string representation of a number to an int[] that contains each digit?

string number = "1234";
number.Array.ConvertAll(line.ToCharArray(), c => Int32.Parse(c.ToString()));
number.ToCharArray().Select(i => Convert.ToInt32(i.ToString())).ToArray();
number.Select(i => Int32.Parse(i.ToString())).ToArray();
number.Select(i => i - '0').ToArray();

Can anyone beat my last approach?

JJS

Posted 2015-07-17T05:44:59.260

Reputation: 181

You might want to not specify the language, as this is CodeGolf. – phase – 2015-07-17T06:02:09.937

1@phase you have to be kidding me. the language is in the title and the tag. – JJS – 2015-07-17T06:04:02.217

2

@phase's comment refers to the fact that 99% of the "questions" on this site are actually programming contests. However, the community has decided that language-specific "tips" questions like this one are also on topic.

– Dennis – 2015-07-17T06:10:12.473

@Dennis thx for the advice. first post on this site, would normally post on stackoverflow, but that community considers code-golf off topic – JJS – 2015-07-17T06:12:07.850

@Dennis I was just about to search meta for that! I do think this would be a nice golf question, if it hasn't been asked already. – phase – 2015-07-17T06:14:15.080

Substract from unicode: https://ideone.com/Z5S43e :D (http://ru.stackoverflow.com/q/446764/178988)

– Qwertiy – 2015-09-07T10:44:47.927

Answers

10

28 bytes

You can make your last approach a bit shorter by replacing the '0' by 48. You can also strip all unnecessary spaces and use 1-char variable names (for example, number -> n):

n.Select(i=>i-48).ToArray();

ProgramFOX

Posted 2015-07-17T05:44:59.260

Reputation: 8 017

Fantastic - really nails the fact that a Char handles arithmatic operators as a Short http://stackoverflow.com/a/1516639/26877

– JJS – 2015-07-17T14:20:47.457

The subtraction could also be replaced by &15 for the same character count. – Peter Taylor – 2015-07-17T16:29:40.687

3

C# without LINQ, 97 86 82 bytes

var n=Console.ReadLine();int i=n.Length;var o=new int[i];while(i>0)o[--i]=n[i]-48;

I've been hanging in the shadows for a while and wanted to try this out. I'm no golfer, or even that experienced with C#, but I wanted to see how small I could get it on my own, without using LINQ. It's nothing special, but at least I had my fun.

I wasn't planning on posting this at first, since it's much longer than any good LINQ solution and doesn't really belong here, but I'm kinda curious if there is a shorter way to do it without LINQ.

Edit 1: Instead of int.Parse it now substracts 48 from the char's ASCII code. Well, that's already much better. 2: Now with var instead of string and int[].

nameless nobody

Posted 2015-07-17T05:44:59.260

Reputation: 31

Using var instead of string and int[] also saves 4. – Peter Taylor – 2015-07-17T19:40:27.867

3

76 bytes

number.ToCharArray().Select(var=>Convert.ToInt32(var.ToString())).ToArray();

user44687

Posted 2015-07-17T05:44:59.260

Reputation: 31