Tutorial by Examples

The trick is to use a look-behind with the regex \G, which means "end of previous match": String[] parts = str.split("(?<=\\G.{8})"); The regex matches 8 characters after the end of the last match. Since in this case the match is zero-width, we could more simply say "...
Same as the known length example, but insert the length into regex: int length = 5; String[] parts = str.split("(?<=\\G.{" + length + "})");

Page 1 of 1