Reputation: 521
I have a int number = 1782901998
whose Length is 10 numbers; I need to split them into 10 different strings. I tried the following code, but it does not give back any output; I need to assign the each number to a string.
string number = 7894;
char[] numberChars = number.ToString().ToCharArray();
int[] digits = new int[numberChars.length];
for(int i = 0; i < numberChars.length; i++) {
digits[i] = (int)numberChars[i];
}
This code only returns 57
in digits list.
Upvotes: 2
Views: 5779
Reputation: 39956
Because your code fills the array with the ASCII code for the characters of the number
variable. You can use LINQ like below:
int[] digits = number.Select(c => Convert.ToInt32(c.ToString())).ToArray();
Or if you want to assign the each number to a string simply:
string[] digits = number.Select(c => c.ToString()).ToArray();
Upvotes: 3