Splitting string into n-length elements for an array

<>

This question already has an answer here:

Answers


You can try this way

String input = "123451234512345";
String[] pairs = input.split("(?<=\\G\\d{5})");
System.out.println(Arrays.toString(pairs));

Output:

[12345, 12345, 12345]

This regex uses positive look behind mechanism (?<=...) and \\G which represents "previous match - place where previously matched string ends, or if it doesn't exist yet (when we just started matching) ^ which is start of the string".

So regex will match any place that has five digits before it and before this five digits previously matched place we split on.


Need Your Help

Objective-C NSString Autorelease Issue

objective-c cocoa

I just spent the last two hours slamming my forehead against my code, trying to figure out what was wrong with it. I eventually narrowed it down to one pair of lines.

How to write xpath for blow code

selenium-webdriver webdriver

There are no of elements in a table this is 43 record and I want to click on this id and for this I am not able to identify the Xpath