C#C
C#2y ago
Penthus

✅ Splitting string into chunks with delimiter

I am trying to split a string to fit within a character limit for my message. This works but cuts words, I was wondering if there is a way to have the chunk split in a way splits the chunk before a word on the new line.

Example data is

John
Ben
Amy
Susan
Bob
Sarah


Current result
John
Ben
Amy
Susan

B
ob
Sarah


Desired Result
John
Ben
Amy
Susan

Bob
Sarah


Current Code
        foreach (var member in members)
        {
            description += $"{member}\n";
        }

        if (description.Length < 4096)
        {
            var message = description;

            PostMessage(message);
        }
        else if (description.Length > 4096)
        {
            var result = description.Chunk(4096).Select(x => new string(x)).ToList();
            foreach (var content in result)
            {
                PostMessage(message);
            }
        }
Was this page helpful?