C#: Cleanest way to divide a string array into N instances N items long

后端 未结 8 1363
我寻月下人不归
我寻月下人不归 2020-12-23 10:40

I know how to do this in an ugly way, but am wondering if there is a more elegant and succinct method.

I have a string array of e-mail addresses. Assume the string

8条回答
  •  失恋的感觉
    2020-12-23 11:09

    It sounds like the input consists of separate email address strings in a large array, not several email address in one string, right? And in the output, each batch is a single combined string.

    string[] allAddresses = GetLongArrayOfAddresses();
    
    const int batchSize = 50;
    
    for (int n = 0; n < allAddresses.Length; n += batchSize)
    {
        string batch = string.Join(";", allAddresses, n, 
                          Math.Min(batchSize, allAddresses.Length - n));
    
        // use batch somehow
    }
    

提交回复
热议问题