Click here to Skip to main content
15,885,309 members
Articles / General Programming / String
Tip/Trick

Simple Word Wrapping in C#

Rate me:
Please Sign up or sign in to vote.
4.50/5 (8 votes)
16 Jun 2020MIT 23.9K   934   11   9
Easily word wrap strings of text in C#
This is just a quick and dirty way to word wrap strings to a specified column width.

Introduction

This is just a little extension method you can use to word wrap text to a specified column width.

Coding this Mess

Using this is pretty simple. You just call the WordWrap() extension method and pass it the column width you'd like.

C#
using System;
using WW;
namespace WordWrapDemo
{
    class Program
    {
        static void Main()
        {
            var text = "The quick brown fox jumped over the lazy dog";
            Console.WriteLine(text.WordWrap(10));
        }
    }
}

This will word wrap the text clamping to 10 characters in width.

Limitations

  • If you have a single word that is greater than the specified column width, it will overrun the column boundary to the right. This is the only case where your text might "spill over" beyond the width specified. This is by design. There's no great way to handle it. The other options would have been to truncate the word, or to hard break the word in the middle of it somewhere.
  • Unfortunately, this does not preserve whitespace, except line breaks. Tabs will be converted to spaces and multiple characters of whitespace will be turned into a single space. Making this preserve whitespace greatly increases the complexity so I decided not to do it.

History

  • 16th June, 2020 - Initial submission

License

This article, along with any associated source code and files, is licensed under The MIT License


Written By
United States United States
Just a shiny lil monster. Casts spells in C++. Mostly harmless.

Comments and Discussions

 
GeneralMy vote of 2 Pin
seanocali2-Aug-21 20:20
seanocali2-Aug-21 20:20 
SuggestionI do not understand very well, the result is strange for me. Pin
Member 373058717-Jun-20 21:50
Member 373058717-Jun-20 21:50 
Questioncomplaint Pin
Daniel Júnior17-Jun-20 9:44
Daniel Júnior17-Jun-20 9:44 
AnswerRe: complaint Pin
honey the codewitch17-Jun-20 9:47
mvahoney the codewitch17-Jun-20 9:47 
GeneralThoughts Pin
PIEBALDconsult17-Jun-20 3:52
mvePIEBALDconsult17-Jun-20 3:52 
GeneralRe: Thoughts Pin
honey the codewitch17-Jun-20 4:05
mvahoney the codewitch17-Jun-20 4:05 
I often only do that for articles, or for really short code in a tip. I guess I could have done it here.

Ah legacy mainframe integration. fun times. Smile | :)
Real programmers use butterflies

GeneralRe: Thoughts Pin
PIEBALDconsult17-Jun-20 4:16
mvePIEBALDconsult17-Jun-20 4:16 
QuestionAbout the limitations... Pin
Member 1330167917-Jun-20 1:39
Member 1330167917-Jun-20 1:39 
AnswerRe: About the limitations... Pin
honey the codewitch17-Jun-20 3:15
mvahoney the codewitch17-Jun-20 3:15 

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.