//使用指定步长走完字符串

//比如 把int数组通过","分割成为字符串。然后每次读取四位,直到读取全部字符串。

public static void testforeachArray() {

        int[] ins = { 1,2,3,4,5,6,7,8,9};
string inStr = string.Join(",", ins);
Console.WriteLine(inStr);

int count = 4;
int length = (int)Math.Ceiling((double)inStr.Length / count);

Console.WriteLine(length); // 5

for (int i = 0; i < length; i++)
{
int start = count * i;
//int end = start + count - 1;
count = i == length-1 ? inStr.Length-i*count : count;
Console.WriteLine(inStr.Substring(start,count));
}
}


结果:

1,2,3,4,5,6,7,8,9

5

1,2,

3,4,

5,6,

7,8,

9