I need to split long sentence into parts preserving whole words. Each part should have given maximum number of characters (including space, dots etc.). For example:
int partLenght = 35;
string sentence = "Silver badges are awarded for longer term goals. Silver badges are uncommon."
Output:
1 part: "Silver badges are awarded for"
2 part: "longer term goals. Silver badges are"
3 part: "uncommon."
While CsConsoleFormat† was primarily designed to format text for console, it supports generating plain text as well.
And, if you actually need trimmed strings like in your question:
In addition to word wrap on spaces, you get proper handling of hyphens, zero-width spaces, no-break spaces etc.
† CsConsoleFormat was developed by me.
This works:
It gives me:
I've been testing Jon's and Lessan's answers, but they don't work properly if your max length needs to be absolute, rather than approximate. As their counter increments, it doesn't count the empty space left at the end of a line.
Running their code against the OP's example, you get:
The "are" on line two, should be on line three. This happens because the counter does not include the 6 characters from the end of line one.
I came up with the following modification of Lessan's answer to account for this:
Expanding on jon's answer above; I needed to switch
g
withg.toArray()
, and also changemax
to(max + 2)
to get an exact wrapping on the max'th character.And here is sample usage as NUnit tests:
Split the string with a
(space), that build up new strings from the resulting array, stopping before your limit for each new segment.
Untested pseudo-code:
I knew there had to be a nice LINQ-y way of doing this, so here it is for the fun of it:
Obviously this code works only as long as the query is not parallel, since it depends on
charCount
to be incremented "in word order".