Click here to Skip to main content
14,574,641 members

Simple Word Wrapping in C#

Rate this:
4.62 (7 votes)
Please Sign up or sign in to vote.
4.62 (7 votes)
16 Jun 2020MIT
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.

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

Share

About the Author

honey the codewitch
United States United States
Just a shiny lil monster. Casts spells in C++. Mostly harmless.

Comments and Discussions

 
SuggestionI do not understand very well, the result is strange for me. Pin
Member 373058717-Jun-20 21:50
MemberMember 373058717-Jun-20 21:50 
Questioncomplaint Pin
Daniel Júnior17-Jun-20 9:44
MemberDaniel 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
professionalPIEBALDconsult17-Jun-20 3:52 
GeneralRe: Thoughts Pin
honey the codewitch17-Jun-20 4:05
mvahoney the codewitch17-Jun-20 4:05 
GeneralRe: Thoughts Pin
PIEBALDconsult17-Jun-20 4:16
professionalPIEBALDconsult17-Jun-20 4:16 
QuestionAbout the limitations... Pin
Member 1330167917-Jun-20 1:39
MemberMember 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.

Tip/Trick
Posted 16 Jun 2020

Stats

6.5K views
363 downloads
8 bookmarked