© 2026 Hedgehog Software, LLC

TwitterGitHubDiscord
More
CommunitiesDocsAboutTermsPrivacy
Search
Star
Setup for Free
C#C
C#•2y ago•
9 replies
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
John
Ben
Amy
Susan
Bob
Sarah


Current result
John
Ben
Amy
Susan

B
ob
Sarah
John
Ben
Amy
Susan

B
ob
Sarah


Desired Result
John
Ben
Amy
Susan

Bob
Sarah
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);
            }
        }
        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);
            }
        }
C# banner
C#Join
We are a programming server aimed at coders discussing everything related to C# (CSharp) and .NET.
61,871Members
Resources

Similar Threads

Was this page helpful?
Recent Announcements

Similar Threads

❔ ✅ Splitting string without allocation
C#CC# / help
3y ago
✅ splitting project into multiple files
C#CC# / help
3y ago
Need help with splitting string using a char in CSharp.
C#CC# / help
4y ago