Convert Overwhelming Information Overload to Information on Demand
Sometimes you need the contents of a long string
, such as an exception message that you want to display in a MessageBox
like so:
catch (Exception x)
{
MessageBox.Show(string.Format("Boo-boo in buttonNoseBest_Click(): {0}", x));
}
But especially if the app throwing the exception is running on a puny device, such as a handheld, you may end up with more information than can fit into the limited real estate of the device's screen. To solve this problem, you can break the long string
into whatever size you want, and then display each portion, one MessageBox.Show
at a time. This code works for that:
const 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.";
int msglen = msg.Length;
int charsDisplayed = 0;
while (charsDisplayed < msglen)
{
int CharsToGrab = MAX_CHARS_TO_SHOW;
int CharsLeft = msglen - charsDisplayed;
if (CharsLeft < MAX_CHARS_TO_SHOW) CharsToGrab = CharsLeft;
string msgepisode = msg.Substring(charsDisplayed, CharsToGrab);
int episodeLen = msgepisode.Length;
MessageBox.Show(msgepisode);
charsDisplayed = charsDisplayed + episodeLen;
}
This will give you the long string
a piece at a time, like so:
Tweak It
You may want less or more information at a time. Just change the value of MAX_CHARS_TO_SHOW
accordingly. You probably also want to work with some string
other than the first part of the first chapter of the first novel (in rank of importance, not chronology), at least first American novel.
Improve It
You may have noticed that the splits are not done on sentences, or even words, but on characters, and so sometimes (usually) the start of a word will appear at the end of one MessageBox.Show()
, and its ending will appear at the beginning of the next one. This works for quick- and-dirty debugging type stuff, but for something "golden," you will want to at least break the long string
into words first or, better yet, sentences. Halve at it!
Apologies / Kudos / Credit to a PropellorHead
A cat calling himself PIEDBALDconsult came up with a more elegant way to do this (using RegEx). You can see it here.
I had a hard time understanding his code at first, but once I saw through the formatting and naming, I finally got it to compile, and it works great - the words are no longer chopped in the middle. Here is my version of his version of my version:
private void buttonItUp_Click(object sender, EventArgs e)
{
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 BreakItUpWithRegex(msg, MAX_CHARS_TO_SHOW))
{
MessageBox.Show(s);
}
}
private static System.Collections.Generic.IEnumerable<string> BreakItUpWithRegex(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;
}</string>
And voila! Now the verbiage is more sensibly split: