65.9K
CodeProject is changing. Read more.
Home

How to Split Long Strings into Manageable Portions and Display them in a MessageBox

starIconstarIconstarIconstarIcon
emptyStarIcon
starIcon

4.71/5 (4 votes)

Jun 16, 2014

CPOL
viewsIcon

18385

This is an alternative for "How to Split Long Strings into Manageable Portions and Display them in a MessageBox"

In the past, I have written code similar to that in the related Tip, but for any who like Regular Expressions, here is a Regular Expression that will split a string into segments with a maximum length, but which has the ability to split on "breaks" so words aren't broken up: (?s).{1,314}(?=(\b|$))

    private static System.Collections.Generic.IEnumerable<string>
    Segment
    (      
      string text
    ,
      int    maxlen
    )
    {
      string regex = System.String.Format ( @"(?s).{{1,{0}}}(?=(\b|$))" , maxlen ) ;
    
      System.Text.RegularExpressions.Regex reg = new System.Text.RegularExpressions.Regex ( regex ) ;
      
      System.Text.RegularExpressions.MatchCollection m = reg.Matches ( text ) ;
            
      for ( int i = 0 ; i < m.Count ; i++ )
      {                                  
        yield return ( m [ i ].Value ) ;       
      }
              
      yield break ;
    }
  
            int MAX_CHARS_TO_SHOW = 314;
            string msg = "YOU don't know about me without you have read a book by the name of The Adventures of Tom Sawyer; but that ain't no matter. " +
                "That book was made by Mr. Mark Twain, and he told the truth, mainly. There was things which he stretched, but mainly he told the truth. "+
                "That is nothing. I never seen anybody but lied one time or another, without it was Aunt Polly, or the widow, or maybe Mary. Aunt Polly -- "+
                "Tom's Aunt Polly, she is -- and Mary, and the Widow Douglas is all told about in that book, which is mostly a true book, with some stretchers, "+
                "as I said before. Now the way that the book winds up is this: Tom and me found the money that the robbers hid in the cave, and it made us rich. "+
                "We got six thousand dollars apiece -- all gold. It was an awful sight of money when it was piled up. Well, Judge Thatcher he took it and put it "+
                "out at interest, and it fetched us a dollar a day apiece all the year round -- more than a body could tell what to do with. The Widow Douglas "+
                "she took me for her son, and allowed she would sivilize me; but it was rough living in the house all the time, considering how dismal regular "+
                "and decent the widow was in all her ways; and so when I couldn't stand it no longer I lit out. I got into my old rags and my sugar-hogshead "+
                "again, and was free and satisfied. But Tom Sawyer he hunted me up and said he was going to start a band of robbers, and I might join if I would "+
                "go back to the widow and be respectable. So I went back. The widow she cried over me, and called me a poor lost lamb, and she called me a lot of "+
                "other names, too, but she never meant no harm by it. She put me in them new clothes again, and I couldn't do nothing but sweat and sweat, and "+
                "feel all cramped up. Well, then, the old thing commenced again. The widow rung a bell for supper, and you had to come to time. When you got to "+
                "the table you couldn't go right to eating, but you had to wait for the widow to tuck down her head and grumble a little over the victuals, "+
                "though there warn't really anything the matter with them, -- that is, nothing only everything was cooked by itself. In a barrel of odds and "+
                "ends it is different; things get mixed up, and the juice kind of swaps around, and the things go better.";

            foreach ( string s in Segment ( msg , MAX_CHARS_TO_SHOW  ) )
            {
              System.Console.WriteLine ( s ) ;
            }