Java Algorithms How to - Split a string at every n-th character


We would like to know how to split a string at every n-th character.


import java.util.ArrayList;
import java.util.List;
//w  ww.j  a  v  a2  s.  c om
public class Main {
  public static void main(String[] args) {
    for (String part : getParts("HelloWorldJava", 3)) {

  private static List<String> getParts(String string, int partitionSize) {
    List<String> parts = new ArrayList<String>();
    int len = string.length();
    for (int i = 0; i < len; i += partitionSize) {
      parts.add(string.substring(i, Math.min(len, i + partitionSize)));
    return parts;

The code above generates the following result.