Here's a solution without regex -
You start at the end of the line, extract 4 or fewer characters and add them to the list:
public static void main (String[] args) {
String stringa = "11111110000000000";
List<String> result = new ArrayList<>();
for (int endIndex = stringa.length(); endIndex >= 0; endIndex -= 4) {
int beginIndex = Math.max(0, endIndex - 4);
String str = stringa.substring(beginIndex, endIndex);
result.add(0, str);
}
System.out.println(result);
}
Result:
[1, 1111, 1100, 0000, 0000]
source
share