(b) have a List<string> result_strings with zero entries
(c) have a String that is empty
(d) foreach through the workingList ... keep adding parts to the empty string as long as it will not exceed 11 bytes in length
(e) if the next part will exceed length == 11, add the current part to your result_strings and start your next part with your String from (c) set back to String.Empty..
(f) watch for the special case where any individual part is >= 11 bytes.
(g) remember to append a space between all concatenated parts.
g.
P.S.: just curious, is this a homework assignment?
B-) Please help me by completing my school survey about computer programmers on my website. Thank you!!! Gerry Lowry +1 705-429-7550 wasaga beach, ontario, canada
Marked as answer by Allen Li - MSFT on May 17, 2012 01:04 AM
gdreedy
Member
482 Points
240 Posts
Split string at spaces by length
May 11, 2012 05:00 PM|LINK
Need help to split string to get List<string> at spaces if the string lenth is >12. words should not be breaked. sub strings should be length <12
Example:
string = "asp.net is a Web app framework developed a"
substrings in the list should be : asp.net is a, Web app, framework, developed a.
Thanks.
gerrylowry
All-Star
20513 Points
5712 Posts
Re: Split string at spaces by length
May 11, 2012 05:26 PM|LINK
@ gdreedy
TIMTOWTDI =. there is more than one way to do it
one approach that comes to mind, in pseudo-code, is this.
(a) create List<String> or String[] workingList as the set of non empty strings from your input string
see http://msdn.microsoft.com/en-us/library/b873y76a.aspx "String.Split Method (Char[])"
(b) have a List<string> result_strings with zero entries
(c) have a String that is empty
(d) foreach through the workingList ... keep adding parts to the empty string as long as it will not exceed 11 bytes in length
(e) if the next part will exceed length == 11, add the current part to your result_strings and start your next part with your String from (c) set back to String.Empty..
(f) watch for the special case where any individual part is >= 11 bytes.
(g) remember to append a space between all concatenated parts.
g.
P.S.: just curious, is this a homework assignment?
Paul Linton
Star
13411 Points
2535 Posts
Re: Split string at spaces by length
May 14, 2012 04:29 AM|LINK
but your first output is 12 characters long. Is the specification correct or your sample?