Wordwrapping in C#(honestillusion.com)

submitted by JamesCurranJamesCurran(635) 5 years, 3 months ago

Taking a block of text, and word-wrap it at a specific line length.

4 comments |category: |Views: 76

tags: another

new Add a live kick counter to your blog >> liveImage

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:

  • "Kick It" text
  • "Kick It" background
  • kick count text
  • kick count background
  • border

Simply copy and paste this HTML into your blog post.


Users who kicked this story:
Comments:

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

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

posted by jfollasjfollas(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

Reply

posted by JamesCurranJamesCurran(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.....)

Reply

information Login or create an account to comment on this story