C#: String Extension SplitOn to Split Text into Specific Sizes

Posted by OmegaMan at August 4, 2010

Category: C#, Regular Expressions

Tags:

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.
 */
Share

10 Comments

  1. Buddhi says

    Nice code, very useful.

    Reply
  2. Bruce says

    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.

    Reply
    • OmegaMan says

      One would have to put in the smarts to handle word processing to better suit your needs. Sorry this doesn’t help.

      Reply
  3. Jaikrishan says

    Nice Code…
    Very useful and save lots of time.
    Thanks,
    Jai

    Reply
  4. Sean says

    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:

    
    string theRegex = string.Format( @"(?.{{1,{1}}})(?:[^\S]+|$)", targetGroup, MaxCharacters );
    
    
    Reply
  5. Abbas says

    This is a fantastic code. Thanks for sharing… it has saved a lot of time.

    Reply
  6. Ana says

    Thanks, Very Useful and save my time.

    Reply
  7. Beth says

    “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.

    Reply
  8. Nanc says

    Thanks so much! Nice, short and concise solution!

    Reply
  9. Ady says

    How about when you have a long string with no space? I tried it and it didn’t split into multiple lines

    Reply

Leave a comment

(required)
(required) (will not be published)

This site uses Akismet to reduce spam. Learn how your comment data is processed.