说我有
var i = 987654321;
是否有一种简单的方法来获取数字数组,等效于
var is = new int[] { 9, 8, 7, 6, 5, 4, 3, 2, 1 };
而不用.ToString()遍历字符int.Parse(x)?
.ToString()
int.Parse(x)
public Stack<int> NumbersIn(int value) { if (value == 0) return new Stack<int>(); var numbers = NumbersIn(value / 10); numbers.Push(value % 10); return numbers; } var numbers = NumbersIn(987654321).ToArray();
无需递归的替代方法:
public int[] NumbersIn(int value) { var numbers = new Stack<int>(); for(; value > 0; value /= 10) numbers.Push(value % 10); return numbers.ToArray(); }