Why do you use a regex for this?
You can do something like this:
String str = "splitstring";
System.out.println("String-length: " + str.length());
for (int i = 0; i < str.length(); i += 2) { // Increments of 2
System.out.print("Split.." + str.charAt(i));
if (i != str.length() - 1) {
System.out.print(str.charAt(i + 1));
}
System.out.println();
}
Output:
String-length: 11
Split..sp
Split..li
Split..ts
Split..tr
Split..in
Split..g
EDIT: If you insist of using the regex though, here it is:
final String str = "splitstring";
System.out.println(Arrays.toString(
str.split("(?<=\\G.{2})")
));
Output remains the same as above. Seems like there is indeed a bug or something, since the regex is the same as in your question.. Can't really say for sure though, since it's not really my expertise.
EDIT 2: Jon Skeet has an alternative method that is more efficient than mine above:
Well, it's fairly easy to do this by brute force:
public static List<String> splitEqually(String text, int size) {
// Give the list the right capacity to start with. You could use an array
// instead if you wanted.
List<String> ret = new ArrayList<String>((text.length() + size - 1) / size);
for (int start = 0; start < text.length(); start += size) {
ret.add(text.substring(start, Math.min(text.length(), start + size)));
}
return ret;
}
I don't think it's really worth using a regex for this.
EDIT: My reasoning for not using a regex:
- This doesn't use any of the real pattern matching of regexes. It's just counting.
- I suspect the above will be more efficient, although in most cases it won't matter
- If you need to use variable sizes in different places, you've either got repetition or a helper function to build the regex itself based on
a parameter - ick.
- The regex provided in another answer firstly didn't compile (invalid escaping), and then didn't work. My code worked first time. That's
more a testament to the usability of regexes vs plain code, IMO.