This code string extension will take a specific number of words and create a list of strings up to that word boundary. 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 theRegex = string.Format( @"(?<{0}>.{{1,{1}}})(?:\W|$)", targetGroup, MaxCharacters );
MatchCollection matches = Regex.Matches( initial, theRegex, RegexOptions.IgnoreCase
| RegexOptions.Multiline
| RegexOptions.ExplicitCapture
| RegexOptions.CultureInvariant
| RegexOptions.Compiled );
if ( matches != null )
if ( matches.Count > 0 )
foreach ( Match m in matches )
lines.Add( m.Groups[targetGroup].Value );
}
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.
*/