Created
April 16, 2024 15:31
-
-
Save VincenzoLaSpesa/3ec8adeeff5c288a7cd470f03a187992 to your computer and use it in GitHub Desktop.
Split a long string in paragraph breaking on spaces
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| /// <summary> | |
| /// Splits a long text into paragraph with a limit on the characters | |
| /// </summary> | |
| /// <param name="text"></param> | |
| /// <param name="maxCharPerParagraph"></param> | |
| /// <returns></returns> | |
| public static List<string> SplitTextIntoParagraphs(string text, int maxCharPerParagraph) | |
| { | |
| List<string> paragraphs = new List<string>(); | |
| int offset = 0; | |
| while(offset < text.Length) | |
| { | |
| //first let's try an hard cut | |
| var textSlice = text[offset..Math.Min(offset+maxCharPerParagraph, text.Length)]; | |
| var tail_len = text.Length - textSlice.Length - offset; | |
| if (tail_len > 0 && textSlice[textSlice.Length - 2] != ' ' && text[offset+ textSlice.Length] != ' ') | |
| { // there is something left, and the cut seems to be in the middle of a sentence | |
| var lastSpaceIndex = textSlice.LastIndexOf(' '); | |
| if (lastSpaceIndex > 0) | |
| { | |
| textSlice = text[offset..(offset + lastSpaceIndex)]; | |
| } | |
| } | |
| offset += textSlice.Length; | |
| paragraphs.Add(textSlice); | |
| } | |
| return paragraphs; | |
| } |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment