C# || Word Wrap – How To Split A String Text Into lines With Maximum Length Using C#
The following is a module with functions which demonstrates how to split text into multiple lines using C#.
The following function is an extension method, which takes a string, and splits it into multiple lines (array indices) of a specified length.
An optional boolean parameter specifies whether this function should break up long individual words to fit as many characters as possible on a line.
With this option enabled, if a word is too long to fit on a line, the word is broken up to fit as many characters as possible up to the maximum length on the current line, and the remaining characters gets moved to the next line (the next array index).
Note: Don’t forget to include the ‘Utils Namespace‘ before running the examples!
1. Word Wrap – Don’t Break Words
The example below demonstrates the use of ‘Utils.Extensions.WordWrap‘ to split a string into multiple lines.
In this example, individual words are not broken up. They are simply moved onto the next line (if necessary).
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 |
// Word Wrap - Don't Break Words using Utils; // Limit string text to 40 characters per line var text = "Lorem ipsum dolor sit amet, consectetur adipiscing elit. Phasellus faucibus venenatis justo, placerat maximus tortor dictum non. Curabitur efficitur pulvinar arcu, id vehicula nisi commodo in. Interdum et malesuada fames ac ante ipsum primis in faucibus. Vivamus consectetur ut orci scelerisque fringilla. Sed id nisl ac nunc congue suscipit at sollicitudin elit. Nullam tempus tortor sit amet molestie rutrum. Sed a sollicitudin magna, eget vulputate tortor. Proin ut sodales orci, a iaculis ipsum.Ut nec odio risus. Vivamus viverra metus turpis. Pellentesque lacinia lacinia tempus. Suspendisse in justo eget orci porttitor ornare vitae nec tortor. Donec dolor odio, dapibus non sodales quis, volutpat a turpis. Curabitur eget fermentum ante. Donec pulvinar nulla non ante mattis aliquam.Aenean vulputate, ligula sit amet laoreet porttitor, magna justo pellentesque diam, id tempus eros orci ut arcu. Praesent et finibus ipsum. Mauris luctus, metus vel sagittis pharetra, felis odio placerat felis, et ullamcorper neque turpis eget tellus. Maecenas vel imperdiet augue. Morbi imperdiet diam ac enim cursus venenatis. Donec eros turpis, pharetra a aliquet sed, vehicula sed libero. Donec efficitur volutpat ex, in euismod dolor tristique at. Quisque a nisi ultricies libero lacinia fringilla nec vel tortor.Aenean id felis et quam lacinia ornare. Curabitur blandit, arcu eget ultricies pulvinar, neque lectus ultrices mauris, vitae feugiat mauris diam id tellus. Vestibulum finibus cursus dui et feugiat. Sed vitae sagittis lorem. Duis suscipit sollicitudin est tempor efficitur. Morbi eget fringilla massa. Integer ullamcorper, ligula congue consequat sodales, dolor eros pretium augue, in luctus enim massa quis est. Fusce cursus ac ipsum quis aliquet. Curabitur pharetra lectus non suscipit scelerisque. Pellentesque vitae scelerisque tellus. Donec sit amet ligula felis. Donec ac hendrerit magna, sed pharetra diam. Morbi gravida felis finibus sodales suscipit. In hac habitasse platea dictumst.Curabitur purus erat, tincidunt vel tincidunt et, fermentum eget dui. Fusce eu justo vel massa ultrices lacinia sit amet quis augue. Nunc fringilla justo quis felis fringilla, at rutrum justo venenatis. Ut ullamcorper gravida finibus. Donec fermentum varius metus sit amet porttitor. Sed at auctor velit. Quisque et urna metus. Donec ut mi vel turpis dignissim pulvinar. Curabitur massa leo, interdum ac urna id, dictum pellentesque nunc. Ut dolor justo, vehicula quis porta in, laoreet eget augue. this is a test pneumonoultramicroscopicsilicovolcanoconiosis"; var maxCharacters = 40; var result = text.WordWrap(maxCharacters); foreach (var line in result) { Console.WriteLine($"Length: {line.Length} - {line}"); } // expected output: /* Length: 39 - Lorem ipsum dolor sit amet, consectetur Length: 35 - adipiscing elit. Phasellus faucibus Length: 40 - venenatis justo, placerat maximus tortor Length: 40 - dictum non. Curabitur efficitur pulvinar Length: 34 - arcu, id vehicula nisi commodo in. Length: 35 - Interdum et malesuada fames ac ante Length: 33 - ipsum primis in faucibus. Vivamus Length: 31 - consectetur ut orci scelerisque Length: 37 - fringilla. Sed id nisl ac nunc congue Length: 37 - suscipit at sollicitudin elit. Nullam Length: 39 - tempus tortor sit amet molestie rutrum. Length: 40 - Sed a sollicitudin magna, eget vulputate Length: 40 - tortor. Proin ut sodales orci, a iaculis Length: 40 - ipsum.Ut nec odio risus. Vivamus viverra Length: 34 - metus turpis. Pellentesque lacinia Length: 36 - lacinia tempus. Suspendisse in justo Length: 36 - eget orci porttitor ornare vitae nec Length: 37 - tortor. Donec dolor odio, dapibus non Length: 32 - sodales quis, volutpat a turpis. Length: 36 - Curabitur eget fermentum ante. Donec Length: 30 - pulvinar nulla non ante mattis Length: 36 - aliquam.Aenean vulputate, ligula sit Length: 35 - amet laoreet porttitor, magna justo Length: 38 - pellentesque diam, id tempus eros orci Length: 35 - ut arcu. Praesent et finibus ipsum. Length: 33 - Mauris luctus, metus vel sagittis Length: 39 - pharetra, felis odio placerat felis, et Length: 37 - ullamcorper neque turpis eget tellus. Length: 35 - Maecenas vel imperdiet augue. Morbi Length: 40 - imperdiet diam ac enim cursus venenatis. Length: 37 - Donec eros turpis, pharetra a aliquet Length: 31 - sed, vehicula sed libero. Donec Length: 39 - efficitur volutpat ex, in euismod dolor Length: 38 - tristique at. Quisque a nisi ultricies Length: 32 - libero lacinia fringilla nec vel Length: 38 - tortor.Aenean id felis et quam lacinia Length: 36 - ornare. Curabitur blandit, arcu eget Length: 32 - ultricies pulvinar, neque lectus Length: 37 - ultrices mauris, vitae feugiat mauris Length: 34 - diam id tellus. Vestibulum finibus Length: 32 - cursus dui et feugiat. Sed vitae Length: 29 - sagittis lorem. Duis suscipit Length: 40 - sollicitudin est tempor efficitur. Morbi Length: 29 - eget fringilla massa. Integer Length: 36 - ullamcorper, ligula congue consequat Length: 37 - sodales, dolor eros pretium augue, in Length: 40 - luctus enim massa quis est. Fusce cursus Length: 32 - ac ipsum quis aliquet. Curabitur Length: 28 - pharetra lectus non suscipit Length: 31 - scelerisque. Pellentesque vitae Length: 34 - scelerisque tellus. Donec sit amet Length: 39 - ligula felis. Donec ac hendrerit magna, Length: 38 - sed pharetra diam. Morbi gravida felis Length: 32 - finibus sodales suscipit. In hac Length: 35 - habitasse platea dictumst.Curabitur Length: 39 - purus erat, tincidunt vel tincidunt et, Length: 38 - fermentum eget dui. Fusce eu justo vel Length: 36 - massa ultrices lacinia sit amet quis Length: 38 - augue. Nunc fringilla justo quis felis Length: 40 - fringilla, at rutrum justo venenatis. Ut Length: 34 - ullamcorper gravida finibus. Donec Length: 31 - fermentum varius metus sit amet Length: 39 - porttitor. Sed at auctor velit. Quisque Length: 37 - et urna metus. Donec ut mi vel turpis Length: 40 - dignissim pulvinar. Curabitur massa leo, Length: 40 - interdum ac urna id, dictum pellentesque Length: 35 - nunc. Ut dolor justo, vehicula quis Length: 39 - porta in, laoreet eget augue. this is a Length: 4 - test Length: 45 - pneumonoultramicroscopicsilicovolcanoconiosis */ |
2. Word Wrap – Break Words
The example below demonstrates the use of ‘Utils.Extensions.WordWrap‘ to split a string into multiple lines.
In this example, individual words are broken up and moved onto the next line (if necessary), with the default hyphen character used, which is a dash (‘-‘).
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 |
// Word Wrap - Break Words using Utils; // Limit string text to 40 characters per line var text = "Lorem ipsum dolor sit amet, consectetur adipiscing elit. Phasellus faucibus venenatis justo, placerat maximus tortor dictum non. Curabitur efficitur pulvinar arcu, id vehicula nisi commodo in. Interdum et malesuada fames ac ante ipsum primis in faucibus. Vivamus consectetur ut orci scelerisque fringilla. Sed id nisl ac nunc congue suscipit at sollicitudin elit. Nullam tempus tortor sit amet molestie rutrum. Sed a sollicitudin magna, eget vulputate tortor. Proin ut sodales orci, a iaculis ipsum.Ut nec odio risus. Vivamus viverra metus turpis. Pellentesque lacinia lacinia tempus. Suspendisse in justo eget orci porttitor ornare vitae nec tortor. Donec dolor odio, dapibus non sodales quis, volutpat a turpis. Curabitur eget fermentum ante. Donec pulvinar nulla non ante mattis aliquam.Aenean vulputate, ligula sit amet laoreet porttitor, magna justo pellentesque diam, id tempus eros orci ut arcu. Praesent et finibus ipsum. Mauris luctus, metus vel sagittis pharetra, felis odio placerat felis, et ullamcorper neque turpis eget tellus. Maecenas vel imperdiet augue. Morbi imperdiet diam ac enim cursus venenatis. Donec eros turpis, pharetra a aliquet sed, vehicula sed libero. Donec efficitur volutpat ex, in euismod dolor tristique at. Quisque a nisi ultricies libero lacinia fringilla nec vel tortor.Aenean id felis et quam lacinia ornare. Curabitur blandit, arcu eget ultricies pulvinar, neque lectus ultrices mauris, vitae feugiat mauris diam id tellus. Vestibulum finibus cursus dui et feugiat. Sed vitae sagittis lorem. Duis suscipit sollicitudin est tempor efficitur. Morbi eget fringilla massa. Integer ullamcorper, ligula congue consequat sodales, dolor eros pretium augue, in luctus enim massa quis est. Fusce cursus ac ipsum quis aliquet. Curabitur pharetra lectus non suscipit scelerisque. Pellentesque vitae scelerisque tellus. Donec sit amet ligula felis. Donec ac hendrerit magna, sed pharetra diam. Morbi gravida felis finibus sodales suscipit. In hac habitasse platea dictumst.Curabitur purus erat, tincidunt vel tincidunt et, fermentum eget dui. Fusce eu justo vel massa ultrices lacinia sit amet quis augue. Nunc fringilla justo quis felis fringilla, at rutrum justo venenatis. Ut ullamcorper gravida finibus. Donec fermentum varius metus sit amet porttitor. Sed at auctor velit. Quisque et urna metus. Donec ut mi vel turpis dignissim pulvinar. Curabitur massa leo, interdum ac urna id, dictum pellentesque nunc. Ut dolor justo, vehicula quis porta in, laoreet eget augue. this is a test pneumonoultramicroscopicsilicovolcanoconiosis"; var maxCharacters = 40; var result = text.WordWrap(maxCharacters, true); foreach (var line in result) { Console.WriteLine($"Length: {line.Length} - {line}"); } // expected output: /* Length: 39 - Lorem ipsum dolor sit amet, consectetur Length: 40 - adipiscing elit. Phasellus faucibus ven- Length: 40 - enatis justo, placerat maximus tortor d- Length: 39 - ictum non. Curabitur efficitur pulvinar Length: 40 - arcu, id vehicula nisi commodo in. Inte- Length: 40 - rdum et malesuada fames ac ante ipsum p- Length: 38 - rimis in faucibus. Vivamus consectetur Length: 40 - ut orci scelerisque fringilla. Sed id n- Length: 40 - isl ac nunc congue suscipit at sollicit- Length: 40 - udin elit. Nullam tempus tortor sit amet Length: 40 - molestie rutrum. Sed a sollicitudin mag- Length: 40 - na, eget vulputate tortor. Proin ut sod- Length: 38 - ales orci, a iaculis ipsum.Ut nec odio Length: 40 - risus. Vivamus viverra metus turpis. Pe- Length: 40 - llentesque lacinia lacinia tempus. Susp- Length: 40 - endisse in justo eget orci porttitor or- Length: 40 - nare vitae nec tortor. Donec dolor odio, Length: 40 - dapibus non sodales quis, volutpat a tu- Length: 40 - rpis. Curabitur eget fermentum ante. Do- Length: 40 - nec pulvinar nulla non ante mattis aliq- Length: 40 - uam.Aenean vulputate, ligula sit amet l- Length: 40 - aoreet porttitor, magna justo pellentes- Length: 38 - que diam, id tempus eros orci ut arcu. Length: 40 - Praesent et finibus ipsum. Mauris luctu- Length: 40 - s, metus vel sagittis pharetra, felis o- Length: 40 - dio placerat felis, et ullamcorper neque Length: 40 - turpis eget tellus. Maecenas vel imperd- Length: 39 - iet augue. Morbi imperdiet diam ac enim Length: 40 - cursus venenatis. Donec eros turpis, ph- Length: 40 - aretra a aliquet sed, vehicula sed libe- Length: 40 - ro. Donec efficitur volutpat ex, in eui- Length: 39 - smod dolor tristique at. Quisque a nisi Length: 38 - ultricies libero lacinia fringilla nec Length: 40 - vel tortor.Aenean id felis et quam laci- Length: 40 - nia ornare. Curabitur blandit, arcu eget Length: 40 - ultricies pulvinar, neque lectus ultric- Length: 39 - es mauris, vitae feugiat mauris diam id Length: 40 - tellus. Vestibulum finibus cursus dui et Length: 39 - feugiat. Sed vitae sagittis lorem. Duis Length: 40 - suscipit sollicitudin est tempor effici- Length: 40 - tur. Morbi eget fringilla massa. Integer Length: 40 - ullamcorper, ligula congue consequat so- Length: 40 - dales, dolor eros pretium augue, in luc- Length: 40 - tus enim massa quis est. Fusce cursus ac Length: 38 - ipsum quis aliquet. Curabitur pharetra Length: 40 - lectus non suscipit scelerisque. Pellen- Length: 38 - tesque vitae scelerisque tellus. Donec Length: 40 - sit amet ligula felis. Donec ac hendrer- Length: 40 - it magna, sed pharetra diam. Morbi grav- Length: 38 - ida felis finibus sodales suscipit. In Length: 39 - hac habitasse platea dictumst.Curabitur Length: 39 - purus erat, tincidunt vel tincidunt et, Length: 38 - fermentum eget dui. Fusce eu justo vel Length: 40 - massa ultrices lacinia sit amet quis au- Length: 40 - gue. Nunc fringilla justo quis felis fr- Length: 38 - ingilla, at rutrum justo venenatis. Ut Length: 40 - ullamcorper gravida finibus. Donec ferm- Length: 38 - entum varius metus sit amet porttitor. Length: 40 - Sed at auctor velit. Quisque et urna me- Length: 40 - tus. Donec ut mi vel turpis dignissim p- Length: 38 - ulvinar. Curabitur massa leo, interdum Length: 40 - ac urna id, dictum pellentesque nunc. Ut Length: 40 - dolor justo, vehicula quis porta in, la- Length: 40 - oreet eget augue. this is a test pneumo- Length: 39 - noultramicroscopicsilicovolcanoconiosis */ |
3. Utils Namespace
The following is the Utils Namespace. Include this in your project to start using!
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 |
// ============================================================================ // Author: Kenneth Perkins // Date: May 7, 2021 // Taken From: http://programmingnotes.org/ // File: Utils.cs // Description: Handles general utility functions // ============================================================================ using System; namespace Utils { public static class Extensions { /// <summary> /// Takes in a string and splits it into multiple lines (array indices) /// of a specified length. If a word is too long to fit on a line, /// the word gets moved to the next line (the next array index) /// </summary> /// <param name="text">The text to word wrap</param> /// <param name="maxCharactersPerLine">The maximum characters per /// line</param> /// <param name="breakWords">Determine if words should fit as many as /// possible on current line, with remaining broken up to the next line</param> /// <param name="hyphenChar">The character to use at the end of the line /// as a hyphen when breaking up words</param> /// <param name="hyphenatedWordMinLength">The minimum length of the /// hyphenated word after it is broken up. A word will not be hyphenated /// unless it meets this minimum length after it is broken up</param> /// <returns>A list of strings that contains each line limited by /// the specified length</returns> public static System.Collections.Generic.List<string> WordWrap(this string text, int maxCharactersPerLine, bool breakWords = false, char? hyphenChar = '-', int hyphenatedWordMinLength = 1) { if (breakWords && hyphenatedWordMinLength >= maxCharactersPerLine) { throw new System.InvalidOperationException($"Unable to fit the value of '{nameof(hyphenatedWordMinLength)}' ({hyphenatedWordMinLength}) with '{nameof(maxCharactersPerLine)}' ({maxCharactersPerLine}). Either increase the value of '{nameof(maxCharactersPerLine)}' or decrease the value of '{nameof(hyphenatedWordMinLength)}'"); } text = text.Trim(); var result = new System.Collections.Generic.List<string>(); if (text.Length <= maxCharactersPerLine) { result.Add(text); } else { var words = text.Split(new char[]{' '}, StringSplitOptions.RemoveEmptyEntries); var leftOver = string.Empty; for (int wordIndex = 0; wordIndex < words.Length || !string.IsNullOrWhiteSpace(leftOver); ++wordIndex) { var currentResultLineIndex = result.Count - 1; var currentResultLineLength = currentResultLineIndex > -1 ? result[currentResultLineIndex].Length : 0; // Start string with left over text from previous word var addition = string.Empty; if (!string.IsNullOrWhiteSpace(leftOver)) { if (currentResultLineLength > 0 && currentResultLineLength < maxCharactersPerLine && !leftOver.StartsWith(" ")) { addition += " "; } addition += leftOver; } // Add current word to string if (wordIndex < words.Length) { var word = words[wordIndex].Trim(); if (currentResultLineLength > 0 && !addition.EndsWith(" ")) { addition += " "; } addition += word; } if (breakWords) { // Determine how many characters to take from the current addition var remainingLineLength = maxCharactersPerLine - currentResultLineLength; var charactersToAddLength = Math.Min((remainingLineLength > 0 ? remainingLineLength : maxCharactersPerLine), addition.Length); // Include hyphen if word will be broken up if (charactersToAddLength > 1 && charactersToAddLength < addition.Length) { var slice = addition.Substring(0, charactersToAddLength); if (!string.IsNullOrWhiteSpace(slice)) { if (hyphenChar == null || slice[charactersToAddLength - 1] != hyphenChar) { // Determine the length of the hyphenated word var hyphenatedWordLength = 0; var hyphenInsertIndexStart = charactersToAddLength - 1; while (hyphenInsertIndexStart >= 0 && !char.IsWhiteSpace(slice[hyphenInsertIndexStart])) { ++hyphenatedWordLength; --hyphenInsertIndexStart; } // Subtract the hypenated character from the result if (hyphenChar.HasValue) { --hyphenatedWordLength; } if (hyphenatedWordLength >= hyphenatedWordMinLength) { addition = addition.Insert(charactersToAddLength - 1, hyphenChar.HasValue ? hyphenChar.ToString() : string.Empty); } else if (hyphenInsertIndexStart >= 0 && (hyphenChar == null || addition[hyphenInsertIndexStart] != hyphenChar)) { addition = addition.Insert(hyphenInsertIndexStart, new string(' ', hyphenatedWordMinLength)); } } } } currentResultLineLength += charactersToAddLength; // Save excess characters from current addition for the next line leftOver = charactersToAddLength < addition.Length ? addition.Substring(charactersToAddLength) : string.Empty; // Only take the specified characters from the string addition = addition.Substring(0, charactersToAddLength); } else { currentResultLineLength += addition.Length; } // Add or append words to result if (result.Count < 1 || currentResultLineLength > maxCharactersPerLine) { // Start new line addition = addition.Trim(); result.Add(addition); } else { // Append existing line result[currentResultLineIndex] += addition; } } } // Remove excess whitespace for (int index = 0; index < result.Count; ++index) { result[index] = result[index].Trim(); } return result; } } }// http://programmingnotes.org/ |
4. More Examples
Below are more examples demonstrating the use of the ‘Utils‘ Namespace. Don’t forget to include the module when running the examples!
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 |
// ============================================================================ // Author: Kenneth Perkins // Date: May 7, 2021 // Taken From: http://programmingnotes.org/ // File: Program.cs // Description: The following demonstrates the use of the Utils Namespace // ============================================================================ using System; using System.Diagnostics; using Utils; public class Program { static void Main(string[] args) { try { // Limit string text to 40 characters per line var text = "Lorem ipsum dolor sit amet, consectetur adipiscing elit. Phasellus faucibus venenatis justo, placerat maximus tortor dictum non. Curabitur efficitur pulvinar arcu, id vehicula nisi commodo in. Interdum et malesuada fames ac ante ipsum primis in faucibus. Vivamus consectetur ut orci scelerisque fringilla. Sed id nisl ac nunc congue suscipit at sollicitudin elit. Nullam tempus tortor sit amet molestie rutrum. Sed a sollicitudin magna, eget vulputate tortor. Proin ut sodales orci, a iaculis ipsum.Ut nec odio risus. Vivamus viverra metus turpis. Pellentesque lacinia lacinia tempus. Suspendisse in justo eget orci porttitor ornare vitae nec tortor. Donec dolor odio, dapibus non sodales quis, volutpat a turpis. Curabitur eget fermentum ante. Donec pulvinar nulla non ante mattis aliquam.Aenean vulputate, ligula sit amet laoreet porttitor, magna justo pellentesque diam, id tempus eros orci ut arcu. Praesent et finibus ipsum. Mauris luctus, metus vel sagittis pharetra, felis odio placerat felis, et ullamcorper neque turpis eget tellus. Maecenas vel imperdiet augue. Morbi imperdiet diam ac enim cursus venenatis. Donec eros turpis, pharetra a aliquet sed, vehicula sed libero. Donec efficitur volutpat ex, in euismod dolor tristique at. Quisque a nisi ultricies libero lacinia fringilla nec vel tortor.Aenean id felis et quam lacinia ornare. Curabitur blandit, arcu eget ultricies pulvinar, neque lectus ultrices mauris, vitae feugiat mauris diam id tellus. Vestibulum finibus cursus dui et feugiat. Sed vitae sagittis lorem. Duis suscipit sollicitudin est tempor efficitur. Morbi eget fringilla massa. Integer ullamcorper, ligula congue consequat sodales, dolor eros pretium augue, in luctus enim massa quis est. Fusce cursus ac ipsum quis aliquet. Curabitur pharetra lectus non suscipit scelerisque. Pellentesque vitae scelerisque tellus. Donec sit amet ligula felis. Donec ac hendrerit magna, sed pharetra diam. Morbi gravida felis finibus sodales suscipit. In hac habitasse platea dictumst.Curabitur purus erat, tincidunt vel tincidunt et, fermentum eget dui. Fusce eu justo vel massa ultrices lacinia sit amet quis augue. Nunc fringilla justo quis felis fringilla, at rutrum justo venenatis. Ut ullamcorper gravida finibus. Donec fermentum varius metus sit amet porttitor. Sed at auctor velit. Quisque et urna metus. Donec ut mi vel turpis dignissim pulvinar. Curabitur massa leo, interdum ac urna id, dictum pellentesque nunc. Ut dolor justo, vehicula quis porta in, laoreet eget augue. this is a test pneumonoultramicroscopicsilicovolcanoconiosis"; var maxCharacters = 40; var result = text.WordWrap(maxCharacters); foreach (var line in result) { Display($"Length: {line.Length} - {line}"); } } catch (Exception ex) { Display(ex.ToString()); } finally { Console.ReadLine(); } } static void Display(string message) { Console.WriteLine(message); Debug.Print(message); } }// http://programmingnotes.org/ |
QUICK NOTES:
The highlighted lines are sections of interest to look out for.
The code is heavily commented, so no further insight is necessary. If you have any questions, feel free to leave a comment below.
Very good, works perfect!
But is there a way to do that with a richtext from richTextBox?