Wordwrapping in C#

« Previous article:   Next article: »
Generics without Collections Blog Home At the Democratic HQ

Some time ago, I needed a function that would take a block of text, and word-wrap it at a specific line length. As apparently you have now done, I googled for it, and found a blog with a seemingly appropriate algorithm.

Except it wasn’t. I immediately noticed that it wasn’t very efficient – but that was merely annoying. It wasn’t grave enough to actually effect the running time of my program noticeably. However, I soon noticed something worse – it just didn’t work. It would occasionally drop the final line of the block. So, I spent an hour or so rewriting it - fixing that problem and giving it a much more efficient algorithm while I was at it. (plus adding a feature I needed for my project).

The key to my speed-up was to copy characters and create new strings as little as possible. In the original, he was concatenating strings merely to measure how long they would be. When a line got to be too long, he throws away the string, and used the save int value of the previous length. This could be don’t not only faster, but also simpler by counting the characters and subtracting.

The revised code is given below. Very little is left from the original (although I think I’m still using some of the original variable names). The key to the speed-up is the avoid creating new string except when absolutely necessary.

kick it on DotNetKicks.com

Tags: