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

后端 未结 8 1391
我寻月下人不归
我寻月下人不归 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 10:58

    I would just loop through the array and using StringBuilder to create the list (I'm assuming it's separated by ; like you would for email). Just send when you hit mod 50 or the end.

    void Foo(string[] addresses)
    {
        StringBuilder sb = new StringBuilder();
    
        for (int i = 0; i < addresses.Length; i++)
        {
            sb.Append(addresses[i]);
            if ((i + 1) % 50 == 0 || i == addresses.Length - 1)
            {
                Send(sb.ToString());
                sb = new StringBuilder();
            }
            else
            {
                sb.Append("; ");
            }
        }
    }
    
    void Send(string addresses)
    {
    }
    

提交回复
热议问题