(See below the break for the next go at this. Leaving this top part here because of thought process...)
Based on my reading of the split()
javadoc, I think I know what's going on.
You want to split the string based on whitespace, up to n times.
String [] m = s.split("\\b", nWords);
Then stitch them back together with token whitespace if you must:
StringBuffer strBuf = new StringBuffer();
for (int i = 0; i < nWords; i++) {
strBuf.append(m[i]).append(" ");
}
Finally, chop that into five equal strings:
String [] out = new String[5];
String str = strBuf.toString();
int length = str.length();
int chopLength = length / 5;
for (int i = 0; i < 5; i++) {
int startIndex = i * chopLength;
out[i] = str.substring(startIndex, startIndex + choplength);
}
It's late at night for me, so you might want to check that one yourself for correctness. I think I got it somewhere in the area code of correct.
OK, here's try number 3. Having run it through a debugger, I can verify that the only problem left is the integer math of slicing strings that aren't factors of 5 into five pieces, and how best to deal with the remaining characters.
It ain't pretty, but it works.
String[] sliceAndDiceNTimes(String victim, int slices, int wordLimit) {
// Add one to the wordLimit here, because the rest of the input string
// (past the number of times split() does its magic) will be in the last
// array member
String [] words = victim.split("\\s", wordLimit + 1);
StringBuffer partialVictim = new StringBuffer();
for (int i = 0; i < wordLimit; i++) {
partialVictim.append(words[i]).append(' ');
}
String [] resultingSlices = new String[slices];
String recycledVictim = partialVictim.toString().trim();
int length = recycledVictim.length();
int chopLength = length / slices;
for (int i = 0; i < slices; i++) {
int chopStartIdx = i * chopLength;
resultingSlices[i] = recycledVictim.substring(chopStartIdx, chopStartIdx + chopLength);
}
return resultingSlices;
}
Important notes:
- "\s" is the correct regex. Using \b ends up with lots of extra splits due to there being word boundaries at the beginning and end of words.
- Added one to the number of times split runs, because the last array member in the String array is the remaining input string that wasn't split. You could also just split the entire string and just use the for loop as-is.
- The integer division remainder is still an exercise left for the questioner. :-)