我需要创建一个接收一串数字的方法——例如,174709580098747434。然后需要将其格式化为三个一组并以数组形式返回——例如,{174、709、580、098,747,434}。
这是我到目前为止所拥有的。我不认为这是在做正确的工作。如果我有像上面这样的非常大的数字,它也不会有用。
我对 C# 很陌生,只是一个初学者!
using System;
namespace ConsoleApp2
{
class Program
{
static void Main(string[] args)
{
string ques = "17470958834";
Console.WriteLine(Chunk(ques));
}
public static string Chunk(string num)
{
// long inInt = Convert.ToInt64(num);
string ans = "";
for (int i = 0; i < num.Length; i += 3)
{
if (i == 2)
{
ans.Insert(2, "-");
}
else if (i == 5)
{
ans.Insert(5, "-");
}
else if (i == 8)
{
ans.Insert(8, "-");
}
else if (i == 11)
{
ans.Insert(11, "-");
}
}
return ans;
}
}
}