Click here to Skip to main content
15,891,657 members
Articles / Programming Languages / C#
Alternative
Tip/Trick

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

Rate me:
Please Sign up or sign in to vote.
4.71/5 (4 votes)
16 Jun 2014CPOL 17.6K   9   14
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|$))

C#
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)


Written By
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

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

"I would be looking for better tekkies, too. Yours are broken." -- Paul Pedant

"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

"Use vertical and horizontal whitespace generously. Generally, all binary operators except '.' and '->' should be separated from their operands by blanks."

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

Comments and Discussions

 
GeneralMy vote of 5 Pin
Lydia Gabriella16-Jul-14 10:55
Lydia Gabriella16-Jul-14 10:55 
GeneralRe: My vote of 5 Pin
PIEBALDconsult16-Jul-14 12:15
mvePIEBALDconsult16-Jul-14 12:15 
GeneralMy vote of 4 Pin
Jason Down19-Jun-14 2:37
Jason 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
mvePIEBALDconsult16-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
mvePIEBALDconsult16-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
mvePIEBALDconsult16-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
mvePIEBALDconsult16-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
mvePete O'Hanlon16-Jun-14 21:29 
GeneralRe: A little too pithy for me Pin
PIEBALDconsult17-Jun-14 5:38
mvePIEBALDconsult17-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.