Click here to Skip to main content
15,940,430 members
Articles / Programming Languages / C# 3.5

Word wrap without cutting words

Rate me:
Please Sign up or sign in to vote.
5.00/5 (4 votes)
7 Jun 2012CPOL1 min read 26.9K   770   15   1
This is an alternative for "Word wrap without cutting words"

Screen image of demo application


This is to present an alternative for Word wrap without cutting words. This simplifies the code using string.Split and LINQ (although I also provide a non-LINQ version for comparison). It also separates the word wrapping funtionality as extension methods in a static class. Finally, I include a WinForms application to demonstrate the code.


The basic idea is to wrap a single line of text (string) to fit a given width, breaking into separate lines only at word boundaries, defined here as "space-delimited". However, the boundary case of a word that is, by itself, longer than the given width is ambiguous. I.e., (a) split the word to fit, or (b) allow single words that are over width. I decided to provide an optional parameter to choose the desired behavior.

The use of string.Split() reduces apparent complexity (especially in the simple case of allowing overwide words) and avoids character-by-character reprocessing when splitting.

Also, instead of coupling to the WinForms TextRenderer and Font classes, the implementation takes a delegate (Predicate<string>) that tests if a string "fits". The caller is free to determine fitting by any means desired.

string input = "This is the test string, with some veeerrrryyy looooong words!";
int wrapWidth = 160;  // determined somehow
// font below determined from some source
Predicate<string> stringFits = s => TextRenderer.MeasureText(s, font).Width < wrapWidth;
string Ret = input.WordWrap(stringFits, false);  // do NOT allow overwide words
// If you will always allow oversize words, then the bool is not required:
string Ret = input.WordWrap(stringFits);  // do allow overwide words

Points of Interest

There are WordWrapNonLinq() methods which implement the same functionality without using the Linq .Aggregate() method. (This demonstrates what the .Aggregate() is actually doing.)


Initial version.


This article, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)

Written By
Software Developer (Senior) Retired
United States United States
I started programming in Basic on a DECSystem-10 as a Freshman at Caltech in 1974. I quickly transitioned to assembly language, Fortran, and Pascal. As a summer job at JPL, I did analysis of fuel consumption for the Viking Mars Orbiter attitude control system. I also spent a summer doing O/S maintenance at Digital Equipment Corporation.
After graduation, I started developing microprocessor development tools (e.g., cross-compiler, debugger) for Beckman Instruments, a scientific instrument company.
I've worked on custom file-systems, a real-time O/S for Z8000, Expert Systems (SpinPro™ & PepPro™), and internal and external networking support (I was their first webmaster).
I've worked on the DNA analysis system.
I was the console/UI software architect for Ultracentrifuges and protein Capillary Electrophoresis (CE) systems.
After 35 years, Danaher having acquired Beckman (now Beckman Coulter), transferred the CE group to become part of Sciex (2014), and was on the software team that developed the new (9/2021) Sciex BioPhase Capillary Electrophoresis instrument.
Finally, after 43 years, 7 months, and 19 days, I am retired.

Comments and Discussions

QuestionMessage Removed Pin
7-Jun-12 15:06
bhjf8dsaf787-Jun-12 15:06 

General General    News News    Suggestion Suggestion    Question Question    Bug Bug    Answer Answer    Joke Joke    Praise Praise    Rant Rant    Admin Admin   

Use Ctrl+Left/Right to switch messages, Ctrl+Up/Down to switch threads, Ctrl+Shift+Left/Right to switch pages.