Formatting an ID string
It is things like this which drive me nuts, and I agree with you, your solution is good, but ugly. Your solution is also probably close to being as efficient as possible in Java, using the StringBuilder, etc.

There are a couple of things through that you can do to xhange the way you code it. Note that your solution may well be the fastest.... (well, it would be the fastest if you specified a starteing capactity of 23 for the StringBuilder like new StringBuilder(23) ...

How about a for-loop:

private static String formatID(String id) {
    StringBuilder sb = new StringBuilder(25);
    for (int i = 0; i &lt; id.length(); i += 4) {
        sb.append(id.substring(i, Math.min(i + 4, id.length())));
        sb.append(" ");
    }
    sb.setLength(23);
    return sb.toString();
}


The last setLength(23) removes the trailing space on the output.

The actual suggestion I prefer, though, is using a format String, like:

private static String formatFID(String id) {
    return String.format("%s %s %s %s %s", id.substring(0, 4), id.substring(4,  8),
            id.substring(8,  12), id.substring(12,  16), id.substring(16, 19));
}


Finally, for completeness, you can actually use a regex to split the input string at 4-char boundaries. Then, in Java 8, stream the results, and join them with a space:

private static final Pattern FOUR = Pattern.compile("(?&lt;=\\G.{4})");

private static String formatRID(String id) {
    return Stream.of(FOUR.split(id)).collect(Collectors.joining(" "));
}


That's a concise way of doing it.....