Click here to Skip to main content
Click here to Skip to main content
Alternative Tip
Go to top

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

, 16 Jun 2014
Rate this:
Please Sign up or sign in to vote.
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 ) ;
            }

License

This article, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)

Share

About the Author

PIEBALDconsult
Software Developer (Senior)
United States United States
BSCS 1992 Wentworth Institute of Technology
 
Originally from the Boston (MA) area. Lived in SoCal for a while. Now in the Phoenix (AZ) area.
 
OpenVMS enthusiast, ISO 8601 evangelist, photographer, opinionated SOB
 
---------------
 
"If you need help knowing what to think, let me know and I'll tell you." -- Jeffrey Snover [MSFT]
 
"Typing is no substitute for thinking." -- R.W. Hamming
 
"I find it appalling that you can become a programmer with less training than it takes to become a plumber." -- Bjarne Stroustrup
 
ZagNut’s Law: Arrogance is inversely proportional to ability.
 
"Well blow me sideways with a plastic marionette. I've just learned something new - and if I could award you a 100 for that post I would. Way to go you keyboard lovegod you." -- Pete O'Hanlon
 
"linq'ish" sounds like "inept" in German -- Andreas Gieriet
 
"Things would be different if I ran the zoo." -- Dr. Seuss
 
"Wrong is evil, and it must be defeated." – Jeff Ello
 
"A good designer must rely on experience, on precise, logical thinking, and on pedantic exactness." -- Nigel Shaw
 
“It’s always easier to do it the hard way.” -- Blackhart

“If Unix wasn’t so bad that you can’t give it away, Bill Gates would never have succeeded in selling Windows.” -- Blackhart

"Omit needless local variables." -- Strunk... had he taught programming
 

 
"We learn more from our mistakes than we do from getting it right the first time."
 
My first rule of debugging: "If you get a different error message, you're making progress."
 
My golden rule of database management: "Do not unto others' databases as you would not have done unto yours."
 
My general rule of software development: "Design should be top-down, but implementation should be bottom-up."

Comments and Discussions

 
GeneralMy vote of 5 PinmemberLydia Gabriella16-Jul-14 10:55 
GeneralRe: My vote of 5 PinmemberPIEBALDconsult16-Jul-14 12:15 
GeneralMy vote of 4 PinmemberJason Down19-Jun-14 2:37 
QuestionA little too pithy for me PinprofessionalB. Clay Shannon16-Jun-14 10:45 
AnswerRe: A little too pithy for me PinmemberPIEBALDconsult16-Jun-14 11:12 
GeneralRe: A little too pithy for me PinprofessionalB. Clay Shannon16-Jun-14 11:28 
GeneralRe: A little too pithy for me PinmemberPIEBALDconsult16-Jun-14 11:54 
GeneralRe: A little too pithy for me PinprofessionalB. Clay Shannon16-Jun-14 12:03 
GeneralRe: A little too pithy for me PinmemberPIEBALDconsult16-Jun-14 12:13 
GeneralRe: A little too pithy for me PinprofessionalB. Clay Shannon16-Jun-14 12:27 
GeneralRe: A little too pithy for me PinmemberPIEBALDconsult16-Jun-14 12:31 
GeneralRe: A little too pithy for me PinprofessionalB. Clay Shannon16-Jun-14 12:50 
GeneralRe: A little too pithy for me PinprotectorPete O'Hanlon16-Jun-14 21:29 
GeneralRe: A little too pithy for me PinmemberPIEBALDconsult17-Jun-14 5:38 

General General    News News    Suggestion Suggestion    Question Question    Bug Bug    Answer Answer    Joke Joke    Rant Rant    Admin Admin   

Use Ctrl+Left/Right to switch messages, Ctrl+Up/Down to switch threads, Ctrl+Shift+Left/Right to switch pages.

| Advertise | Privacy | Mobile
Web02 | 2.8.140926.1 | Last Updated 16 Jun 2014
Article Copyright 2014 by PIEBALDconsult
Everything else Copyright © CodeProject, 1999-2014
Terms of Service
Layout: fixed | fluid