7

Possible Duplicate:
Split string to equal length substrings in Java

Given the following utility method I have:

/**
 * Splits string <tt>s</tt> into chunks of size <tt>chunkSize</tt>
 *
 * @param s the string to split; must not be null
 * @param chunkSize number of chars in each chuck; must be greater than 0
 * @return The original string in chunks
 */
public static List<String> splitInChunks(String s, int chunkSize) {
    Preconditions.checkArgument(chunkSize > 0);
    List<String> result = Lists.newArrayList();
    int length = s.length();
    for (int i = 0; i < length; i += chunkSize) {
        result.add(s.substring(i, Math.min(length, i + chunkSize)));
    }
    return result;
}

1) Is there an equivalent method in any common Java library (such as Apache Commons, Google Guava) so I could throw it away from my codebase? Couldn't find with a quick look. Whether it returns an array or a List of Strings doesn't really matter.

(Obviously I wouldn't add dependency to some huge framework just for this, but feel free to mention any common lib; maybe I use it already.)

2) If not, is there some simpler and cleaner way to do this in Java? Or a way that is strikingly more performant? (If you suggest a regex-based solution, please also consider cleanness in the sense of readability for non regex experts... :-)

Edit: this qualifies as a duplicate of the question "Split string to equal length substrings in Java" because of this Guava solution which perfectly answers my question!

Community
  • 1
  • 1
Jonik
  • 80,077
  • 70
  • 264
  • 372
  • I agree it's a duplicate. Or, rather, the question isn't an exact dupe, but the answers contain your [beautiful Guava solution](http://stackoverflow.com/questions/3760152/split-string-of-equal-lengths-in-java/3760862#3760862) which is exactly what I want! – Jonik Nov 04 '10 at 14:42
  • @seanizer: I did look at Guava's `Strings` class, but I had completely missed the existence of `Splitter`. – Jonik Nov 04 '10 at 14:46

2 Answers2

16

You can do this with Guava's Splitter:

 Splitter.fixedLength(chunkSize).split(s)

...which returns an Iterable<String>.

Some more examples in this answer.

Community
  • 1
  • 1
Sean Patrick Floyd
  • 292,901
  • 67
  • 465
  • 588
  • I don't deserve the credit. I ripped that right out of the OP's code. – jjnguy Nov 04 '10 at 14:41
  • Of course, I'd dump my own utility method and just use Splitter directly now that I'm aware of it, but thanks for pointing out Guava can do this! – Jonik Nov 04 '10 at 15:02
  • Heh, this is going to be closed, and the answer I'd like to accept is [this](http://stackoverflow.com/questions/3760152/split-string-to-equal-length-substrings-in-java/3760862#3760862)... Hmm, but wait, let me edit this answer to my liking first, and the I'll give you the extra points. :-) – Jonik Nov 04 '10 at 15:14
1

Mostly a duplicate of Split Java String in chunks of 1024 bytes where the idea of turning it into a stream and reading N bytes at a time would seem to meet your need?

Here is a way of doing it with regex (which seems a bit of a sledgehammer for this particular nut)

Community
  • 1
  • 1
The Archetypal Paul
  • 41,321
  • 20
  • 104
  • 134