Piet Souris wrote:Or alternatively, start with s.substring(0, N).lastIndexOf(" "), and so on. Using a java 8 stream would make for a short code.
Piet Souris wrote:There is one nasty case: if there is a space, say at index 3, and there is a space at index 40, then the split should happen at index 40, instead of index 3 That's why I added a check for such a situation. See the String that I used. This check should also be added to Carey's code.
Anyone interested in a Stream version?
Piet Souris wrote:Gosh,
hadn't considered newlines as part of the string, let alone \r\n....
My idea was to do an IntStream from 0 to s.length(), filtering for charAt is a whitespace, so that we get a list of candidate breakpoints, Combining this with the maxLength and a Stream.iterate(), we should be able to come up with a definitive list of breakpoints. I would first start with replacing every sequence of two or more whitespaces with just a single space, to simplify things.
I thought initially to be a simple job, but now I say: lets wait for OP to come up with details about what horrors the input string can have.
Carey Brown wrote:Streams...
The String class has a chars() method that creates a stream of characters as ints. What I want to put after that is a black box that keeps taking in characters and buffers them and has a state machine to determine when it needs to take some of the buffer and output a Stream of Strings. I'm not familiar with any Stream operations that take one Stream and turn them into another Stream that has a different rate of input to output.
Farmers know to never drive a tractor near a honey locust tree. But a tiny ad is okay:
global solutions you can do at home or in your backyardhttps://www.kickstarter.com/projects/paulwheaton/better-world-boo