yesthatmcgurk


Comments:

Wordwrapping in C#

posted by yesthatmcgurkyesthatmcgurk(4063) 5 years, 3 months ago 0

This is a good starting point; however, the code has been mangled by a smiley translator (an "idea" bulb has been inserted for some text). I personally would use regex to do this. You can finely tune the splitting logic. Here are some of the regex strings you can use to split:
// point at which we want to place our breaks
int length = 5;
// an absolute maximum line length; forces hard break at this point
int maxLength = 8;

// Absolute hard break every {0} chars
string hardBreakSpacesIncluded = string.Format("(.{{0,{0}}})",length);
// Hard break after {0} chars excluding whitespace
string hardBreakWithTrailingWhitespace = string.Format("(.{{0,{0}}}\\s*)", length);
// Hard break after {0} chars discarding whitespace past {0} characters
string hardBreakWithoutTrailingWhitespace = string.Format("(.{{0,{0}}})\\s*", length);
// Soft break after {0} chars including trailing whitespace"
string softBreakInclusive = string.Format("(.{{0,{0}}}\\S*\\s*)",length);
// Soft break after {0} chars excluding trailing whitespace
string softBreakExclusive = string.Format("(.{{0,{0}}}\\S*)\\s*", length);
// Soft break after {0} chars and a hard max of {1} chars including trailing whitespace
string softBreakWithSoftMax = string.Format("(.{{0,{0}}}\\S{{0,{1}}}\\s*)", length, maxLength - length);
// Soft break after {0} chars and a hard max of {1} chars excluding trailing whitespace
string softBreakWithHardMax = string.Format("(.{{0,{0}}}\\S{{0,{1}}})\\s*", length, maxLength - length);

Reply

Wordwrapping in C#

posted by yesthatmcgurkyesthatmcgurk(4063) 5 years, 3 months ago 0

Sorry, in comments replace {0} with length and {1} with maxLength, and use Regex.Split(stringToSplit,regexString) to split into an array of strings wrapped at the designated lengths.

Reply