Wordwrapping in C#(honestillusion.com)
submitted by JamesCurran(635) 5 years, 3 months ago
Taking a block of text, and word-wrap it at a specific line length.
4 comments |category: C# |Views: 76 Tweet
tags: JamesCurran string C# another
Add a live kick counter to your blog >>
You can even customize the image by choosing your own colors, and then clicking the button below to update the preview and the html code:
Simply copy and paste this HTML into your blog post.
posted by yesthatmcgurk(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
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.
posted by jfollas(425) 5 years, 3 months ago 0
Gotta chime in with my version, which simply inserts breaks into a stringbuilder: http://jasonf-blog.blogspot.com/2005/07/word-wrap-version-2.html
posted by JamesCurran(635) 5 years, 3 months ago 0
Jason: In mine I was trying for the best efficiency. Mine is basically O(n) - Once the StringBuilder is created, each character is copied only once. Every time you call Insert in yours, every character after the insertion point has to be moved down, making it O(n*n) (Of course, the Replace()s at the top of mine kinda screws up the purity of mine, but I'm workin on that.....)
All tags Your tags
Suggest a new category
dpeterson(1273)
Telerik(409)
fdub(381)
amit.jain(268)
BlackWasp(259)
RobertTheGrey(259)
jalpesh(252)
bsenoff(240)
KodefuGuru(227)
KMillerr(226)