for example suppose that I have a string like "231143" and I wanna convert it to an int[] array which I can easily access 2,3,1,1,4,3 as an int. What should I do?
Asked
Active
Viewed 84 times
1 Answers
1
Without any error checking you can do:
var value = "231143";
var array = value.Select(c => c - '0').ToArray();
This makes use of a trick whereby you can subtract '0'
from the value of a single character holding a number to get its integer value.

Sean
- 60,939
- 11
- 97
- 136
-
Note that this solution is only correct if it is guranteed that the input string **only** contains digits – Ackdari May 07 '20 at 13:39
-
1@Ackdari I think we can safely assume this given by the wording of the question. – Fildor May 07 '20 at 13:41
-