C#: String Extension SplitOn to Split Text into Specific Sizes
Update 7/11/2016 Updated Code To use Linq
This code string extension which will take a specific max line size and fit only words into those lines.
For example if one split on this sentence: “The Fall River” would make three strings of “The “, “Fall “ and “River”.
I have placed this into a string extension which could be called such as
List<string> items = "The Fall River".SplitOn( 5 );
Here is the extension method
// using System.Text.RegularExpressions; public static class StringExtensions { /// <summary>Use this function like string.Split but instead of a character to split on, /// use a maximum line width size. This is similar to a Word Wrap where no words will be split.</summary> /// Note if the a word is longer than the maxcharactes it will be trimmed from the start. /// <param name="initial">The string to parse.</param> /// <param name="MaxCharacters">The maximum size.</param> /// <remarks>This function will remove some white space at the end of a line, but allow for a blank line.</remarks> /// /// <returns>An array of strings.</returns> public static List<string> SplitOn( this string initial, int MaxCharacters ) { List<string> lines = new List<string>(); if ( string.IsNullOrEmpty( initial ) == false ) { string targetGroup = "Line"; string pattern = string.Format( @"(?<{0}>.{{1,{1}}})(?:\W|$)", targetGroup, MaxCharacters ); lines = Regex.Matches(initial, pattern, RegexOptions.Multiline | RegexOptions.CultureInvariant ) .OfType<Match>() .Select(mt => mt.Groups[targetGroup].Value) .ToList(); } return lines; } }
Here is an example with output of its usage:
string text = "The rain in spain falls mainly on the plain of Jabberwocky falls."; List<string> lines = text.SplitOn( 20 ); foreach ( string line in lines ) Console.WriteLine( line ); /* The rain in spain falls mainly on the plain of Jabberwocky falls. */ foreach ( string line in text.SplitOn( 11 ) ) Console.WriteLine( line ); /* The rain in spain falls mainly on the plain of Jabberwocky falls. */
Nice code, very useful.
Nice code and just what i am looking for, BUT is it possible to NOT trim the word from the start, i am looking at trying to place long words in a small text label so i need to break the long words into lines but i don’t want to loose and letters from the words.
One would have to put in the smarts to handle word processing to better suit your needs. Sorry this doesn’t help.
Nice Code…
Very useful and save lots of time.
Thanks,
Jai
Thanks for the great code. It didn’t handle words starting with double-quotes so I modified the regex to break on any white-space character:
This is a fantastic code. Thanks for sharing… it has saved a lot of time.
Thanks, Very Useful and save my time.
“Thanks for the great code. It didn’t handle words starting with double-quotes so I modified the regex to break on any white-space character:” That completely saved my Sean, thanks for that.
Thanks so much! Nice, short and concise solution!
How about when you have a long string with no space? I tried it and it didn’t split into multiple lines