String largeString = "This is a very large and totally useless and meaningless string"; int chunkLength = 5; String[] chunks = largeString.split("(?<=\\G.{" + chunkLength + "})"); System.out.println("Number of chunks: " + chunks.length); // Should print 13 System.out.println("Chunk size: " + chunks[0].size()); // Should print 5 == chunkLengthThe regular expression is a Positive Look-Behind looking for any chunkLength characters beginning at the position where the last match ended. So the first time around, it matches the beginning of the string and then after that, it keeps matching every set of chunkLength characters.
This makes me think I should write a more detailed post about regular expressions, especially on Look-Ahead and Look-Behind - stay tuned!