A specialized String.Split but on a location instead of a character
-
Tuesday, September 12, 2006 10:02 PMModeratorI am looking to split a string at a specific character count and return an array of strings from that, with the smarts of not splitting on a word....
The functionality would somewhat mirror string.Split which is used to tokenize, but instead of a character it would have a hard minimum line size.
Any existing functionality to tap into or thoughts on doing it?
advTHANKSance
Example
string it = "A post to the forums can be helpful";
strings[] lines = it.splitOnLocation(10);
Produces an array {"A post to " , "the forums" , " can be " , "helpful" }
All Replies
-
Wednesday, September 13, 2006 2:09 PM
Um... A hard minimum line size, or a hard maximum? (your description & example disagree)
For a hard max, do a google search for "C# Word Wrap" (and if I remember, I'll post mine to my blog -- if I forget, I'm sure there are others out there)
-
Wednesday, September 13, 2006 2:21 PMModeratorThanks James,
My post could use tweaking to make it a better design document <g>.
Word wrap is the key as with laying out text on a page so to speak...one would not want chop a word in the middle but move it down to the next line. So I guess the rule would be no more words over the line size with the knowledge that lines returned would be smaller or equal to the total characters specified.
My post here on the MS forum is primarily to find out if I have overlooked any Microsoft C#/.Net functionality within the framework which speaks to this issue, either directly or indirectly.
Thanks, -
Wednesday, September 13, 2006 11:19 PM
You might consider using a Regular Expression... the following is quite short should work as you specified:
string test = "this is an example using a regular expression to wrap some text in lines of ten chars.";
MatchCollection matches = Regex.Matches (test, @".{1,10}(\s|$)");
foreach (Match m in matches) {
Console.WriteLine (m.Value);
}This won't work properly if you expect to have words longer than 10 characters: you will only get the last ten chars. But as you didn't provide a specification for that case I'm allowed to assume it as implementation dependent ;)
HTH
--mc -
Thursday, September 14, 2006 8:08 AMModerator

