Click here to Skip to main content
12,453,672 members (61,709 online)
Click here to Skip to main content
Add your own
alternative version

Stats

5.7K views
8 bookmarked
Posted

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

, 16 Jun 2014 CPOL
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, acknowledged pedant and contrarian

---------------

"Using fewer technologies is better than using more." -- Rico Mariani

"Good code is its own best documentation. As you’re about to add a comment, ask yourself, ‘How can I improve the code so that this comment isn’t needed?’" -- Steve McConnell

"Every time you write a comment, you should grimace and feel the failure of your ability of expression." -- Unknown

"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

You may also be interested in...

Pro
Pro

Comments and Discussions

 
GeneralMy vote of 5 Pin
Lydia Gabriella16-Jul-14 10:55
memberLydia Gabriella16-Jul-14 10:55 
GeneralRe: My vote of 5 Pin
PIEBALDconsult16-Jul-14 12:15
memberPIEBALDconsult16-Jul-14 12:15 
GeneralMy vote of 4 Pin
Jason Down19-Jun-14 2:37
memberJason Down19-Jun-14 2:37 
QuestionA little too pithy for me Pin
B. Clay Shannon16-Jun-14 10:45
professionalB. Clay Shannon16-Jun-14 10:45 
AnswerRe: A little too pithy for me Pin
PIEBALDconsult16-Jun-14 11:12
memberPIEBALDconsult16-Jun-14 11:12 
GeneralRe: A little too pithy for me Pin
B. Clay Shannon16-Jun-14 11:28
professionalB. Clay Shannon16-Jun-14 11:28 
GeneralRe: A little too pithy for me Pin
PIEBALDconsult16-Jun-14 11:54
memberPIEBALDconsult16-Jun-14 11:54 
GeneralRe: A little too pithy for me Pin
B. Clay Shannon16-Jun-14 12:03
professionalB. Clay Shannon16-Jun-14 12:03 
GeneralRe: A little too pithy for me Pin
PIEBALDconsult16-Jun-14 12:13
memberPIEBALDconsult16-Jun-14 12:13 
GeneralRe: A little too pithy for me Pin
B. Clay Shannon16-Jun-14 12:27
professionalB. Clay Shannon16-Jun-14 12:27 
GeneralRe: A little too pithy for me Pin
PIEBALDconsult16-Jun-14 12:31
memberPIEBALDconsult16-Jun-14 12:31 
GeneralRe: A little too pithy for me Pin
B. Clay Shannon16-Jun-14 12:50
professionalB. Clay Shannon16-Jun-14 12:50 
GeneralRe: A little too pithy for me Pin
Pete O'Hanlon16-Jun-14 21:29
protectorPete O'Hanlon16-Jun-14 21:29 
GeneralRe: A little too pithy for me Pin
PIEBALDconsult17-Jun-14 5:38
memberPIEBALDconsult17-Jun-14 5:38 

General General    News News    Suggestion Suggestion    Question Question    Bug Bug    Answer Answer    Joke Joke    Praise Praise    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 | Terms of Use | Mobile
Web02 | 2.8.160826.1 | Last Updated 16 Jun 2014
Article Copyright 2014 by PIEBALDconsult
Everything else Copyright © CodeProject, 1999-2016
Layout: fixed | fluid