C# 有没有一种简单的方法可以将 int 转换为每个数字的 int 数组?

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/829174/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-05 03:18:18  来源:igfitidea点击:

Is there an easy way to turn an int into an array of ints of each digit?

c#

提问by Andrew Bullock

Say I have

说我有

var i = 987654321;

Is there an easy way to get an array of the digits, the equivalent of

有没有一种简单的方法来获取数字数组,相当于

var is = new int[] { 9, 8, 7, 6, 5, 4, 3, 2, 1 };

without .ToString()ing and iterating over the chars with int.Parse(x)?

没有.ToString()ing 和迭代字符int.Parse(x)

采纳答案by Garry Shutler

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();

Alternative without recursion:

没有递归的替代方案:

public int[] NumbersIn(int value)
{
    var numbers = new Stack<int>();

    for(; value > 0; value /= 10)
        numbers.Push(value % 10);

    return numbers.ToArray();
}

回答by Grzegorz Gierlik

In short: use loop which divide number modulo 10 (%) to get reminder (each digit) and put it into array.

简而言之:使用将数字除以 10 (%) 的循环来获取提醒(每个数字)并将其放入数组。

回答by marklam

var x = new Stack<int>();
do
{
    x.Push(i % 10);
    i /= 10;
} while (i > 0);
return x.ToArray();

回答by Peter Lillevold

Another alternative which don't uses recursion and uses a Stack that avoids reallocation on every insert (at least for the first 32 digits):

另一种不使用递归并使用避免在每次插入时重新分配的堆栈的替代方法(至少对于前 32 位数字):

var list = new Stack<int>(32);
var remainder = 123456;
do
{
    list.Push(remainder % 10);
    remainder /= 10;
} while (remainder != 0);

return list.ToArray();

And yes, this method also works for 0 and negative numbers.

是的,这种方法也适用于 0 和负数。

Interestingly, give this algorithm a negative number -123456 and you will get {-1, -2, -3, -4, -5, -6}

有趣的是,给这个算法一个负数 -123456,你会得到 {-1, -2, -3, -4, -5, -6}

Update: switched from using List to Stack since this automatically gives the correct order.

更新:从使用列表切换到堆栈,因为这会自动给出正确的顺序。

回答by Joel Coehoorn

This does convert to string and iterate over the characters, but it does it sort of automatically and in a one-liner:

这确实会转换为字符串并遍历字符,但它会自动地以单行方式进行:

var i = 987654321;
var @is = i.ToString().Select(c => c - '0').ToArray();

回答by Svish

I know there are probably better answers than this, but here is another version:

我知道可能有比这更好的答案,但这是另一个版本:

You can use yield returnto return the digits in ascending order (according to weight, or whatever it is called).

您可以使用yield return以升序返回数字(根据重量或任何名称)。

public static IEnumerable<int> Digits(this int number)
{
    do
    {
        yield return number % 10;
        number /= 10;
    } while (number > 0);
}

12345 => 5, 4, 3, 2, 1

12345 => 5, 4, 3, 2, 1

回答by Matthew Whited

Strings and can fun (some of the other options would be faster... but this is pretty easy)

字符串和可以有趣(其他一些选项会更快......但这很容易)

var @is = 987654321.ToString().Select(c => c - 48).ToArray();

回答by Alex Butenko

.NET 4.7.1 or above:

.NET 4.7.1 或更高版本:

IEnumerable<long> GetDigits(long value) =>
  value == 0 ? new long[0] : GetDigits(value / 10).Append(value % 10)

.NET 3.5 - 4.7:

.NET 3.5 - 4.7:

IEnumerable<long> GetDigits(long value) =>
  value == 0 ? new long[0] : GetDigits(value / 10).Concat(new[] { value % 10 });